Geeks With Blogs
Josh Reuben

 

If you hate being a mindless drone in some Illogical software sweatshop, switch to T4.

Note that you cannot use reflection - you must use EnvDTE or your VS instance will lock.

Use the T4Toolbox to access the current TransformContext to pull classes from files.

Apply an iteration to generate properties in the target class that use the model source class for backing properties and change notification.

It just works!

 

<#@ templatedebug="true" hostSpecific="true" #>
<#@ outputextension=".cs" #>
<#@ AssemblyName="System.Core.dll" #>
<#@ AssemblyName="System.Windows.Forms.dll" #>
<#@ importnamespace="System" #>
<#@ importnamespace="System.IO" #>
<#@ importnamespace="System.Diagnostics" #>
<#@ importnamespace="System.Linq" #>
<#@ importnamespace="System.Collections" #>
<#@ importnamespace="System.Collections.Generic" #>
<#@ includefile="T4Toolbox.tt" #>
<#@ importnamespace="EnvDTE" #>
<#
// insert your template code here the tempalte code will be syntaxhighlighted
// and you will have intellisense for all namespaces in the full edition
CodeClass codeClass = GetClass("MyModel.cs");
if (codeClass != null)
{  
       Debug.Print(codeClass.Name);
       //string model = codeClass.Name;
 
// This is the output code from your template
// you only get syntax-highlighting here - not intellisense
#>
using System;
using System.ComponentModel;
using <#= codeClass.Namespace.FullName #>;
 
namespace MyNameSpace
{
       publicclass <#= codeClass.Name #>ViewModel : INotifyPropertyChanged
       {
      
             privatereadonly <#= codeClass.Name #> _model;
             public <#= codeClass.Name #>ViewModel (<#= codeClass.Name #> model)
             {
                    _model = model;
             }
            
             publicevent PropertyChangedEventHandler PropertyChanged;
            
             privatevoid NotifyPropertyChanged(String info)
       {
             if (PropertyChanged != null)
               {
                   PropertyChanged(this, new PropertyChangedEventArgs(info));
               }
       }
 
      
<#          
             foreach(var x in codeClass.Members)
             {
                    var p = x as CodeProperty;
                    Debug.Print("{0}:{1}", p.Name , p.Type.AsString); // I see this in output window whenever I save the tt
#>
             public <#= p.Type.AsString #> <#= p.Name #>
             {
                    get
                    {
                           return _model.<#= p.Name #>;
                    }
                    set
                    {
                           _model.<#= p.Name #> = value;
                           NotifyPropertyChanged("<#= p.Name #>");
                    }
             }                               
<#          
             }
      
      
      
      
#>
 }
}
 
<#
       }     
#>
 
<#+
 // Insert any template procedures here
      
 
 private CodeClass GetClass(string file)
 {
       ProjectItem projectItem = TransformationContext.FindProjectItem(file);
       FileCodeModel codeModel = projectItem.FileCodeModel;
       return FindClass(codeModel.CodeElements);
 }
 
  
 private CodeClass FindClass(CodeElements elements)
 {
       foreach (CodeElement element in elements)
       {
        CodeClass codeClass = element as CodeClass;
        if (codeClass != null) return codeClass;
        codeClass = FindClass(element.Children);
        if (codeClass != null) return codeClass;
       }
      
       returnnull;
 }
      
#>
 

 

 

Posted on Wednesday, March 30, 2011 8:49 PM Meta-Programming , XAML | Back to top


Comments on this post: Stop writing ViewModels and start using T4 !!!!

# re: Stop writing ViewModels and start using T4 !!!!
Requesting Gravatar...
Thanks for the idea. I think, it is a better option to take. - Paradise Home Improvement Charlotte
Left by Ema Wayne on Dec 22, 2016 11:38 AM

Your comment:
 (will show your gravatar)


Copyright © JoshReuben | Powered by: GeeksWithBlogs.net