T4 / Code Generation


Published on

This presentation was made to the Kitchener .NET UG on Feb. 23, 2011

Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Characteristics of the transformed outputDebug --- true/falseTrue – you can take a look files in system %TEMP% folderLanguage: Values: C#, VB, C#3.5InheritsBase class: Microsoft.VisualStudio.TextTemplating.TextTransformationTells which base class to useHostspecific --- true/falseMostly used to output template file name to the transformed fileHost.TemplateFileHost.StandardAssemblyReferenes
  • “Add reference” in VSOne parameternamecould be an assembly in the GAC<#@ assembly name=“System.Data” #>or absolute path to the assembly<#@ assembly name=“c:\\projects\\proj1\\bin\\debug\\proj1.dll” #>
  • Equivalent to: C# using VB.NET importOne parameternamespace
  • File to be included verbatim in templateOne parameterFileCan contain any of the valid T4 constructsRelative paths -- to the main template
  • Just type directly into the .tt file Copied directly to TransformText method%TEMP%Show generated file
  • Control the flow of processing in TransformText methodExample usage:To conditionally output Text blocksIterate over data and output things conditionally
  • Use to add strings to the output fileAdded, in sequenceTo the “TransformText” method with calls to the ToString() methodUsed in cases to change some aspect of the generated output text file to useCalculated valuesExternal data e.g. database column valuessome other calculated value in a Statement block somewhere else in the .TT file. C# – could be multiple linesVB.NET – single lines only
  • T4 / Code Generation

    1. 1. T4 / Code Generation<br />Presented by Joubin Najmaie (@codekindler)<br />February 23, 2011<br />Kitchener .NET UG: @cttdnug<br />
    2. 2. Agenda<br />Who am I?<br />Motivations Behind Using T4<br />T4 Syntax<br />Debugging T4 Templates<br />Break<br />Custom T4 Host using GAX<br />Compiling Generated Code<br />T4 Item Templates<br />VS 2010 Item/Project Templates<br />Resources<br />Q&A<br />
    3. 3. Who am I?<br />Technical Architect at one of the largest IT solution companies in North America<br />Project: www.pagefragments.com<br />Blog: www.codekindler.com<br />Twitter: @codekindler<br />Email: joubin.najmaie@gmail.com<br />Who am I?<br />
    4. 4. Motivations Behind Using T4<br />What is T4?<br />Text Template Transformation Toolkit<br />Transformation engine<br />Vehicle to Generates code from described models<br />Models could be<br />Visual<br />Class diagrams<br />ERDs, Entity Framework<br />Text-based<br />XSD<br />Metadata<br />
    5. 5. Motivations behind using T4<br />Why T4 is important?<br />Developer guidance + speed<br />Not limited to code: any text-based asset<br />Documentation generation<br />Scaffolding applications<br />ORMs<br />Domain Specific Development<br />Extending Visual Studio<br />Item Templates<br />Including latest ASP.NET MVC Controller and View templates<br />Project Templates<br />
    6. 6. Motivations Behind Using T4<br />How I got to work with T4<br />Consultancy work – worked on a PoC migration of a static site to a content managedwith 1000+ pages<br />Worked with a couple of CMSs – SharePoint, Sitecore and an in-house CMS at an agency firm<br />Patterns started emerging<br />Create fragments of a site that are most needed to be managed more regularly than others by different owners<br />The fragments in the system are compiled and included using T4<br />Addressing shortcomings and pains of CMSs<br />
    7. 7. T4 Syntax<br />
    8. 8. T4 Syntax<br />Anatomy of T4 templates<br />
    9. 9. T4 Syntax<br />Text Templating ‘workflow’<br />
    10. 10. T4 Syntax<br />Processing Directives<br /> Similar to ASPX or ASCX<br /> <#@ and end with #><br /> Directives<br /> template<br /> assembly<br /> import<br /> output<br /> include<br />
    11. 11. T4 Syntax<br />template directive<br /> <#@ template debug="true" language="C#" #><br /> Parameters<br />debug<br />language<br />inherits<br />hostspecific<br />
    12. 12. T4 Syntax<br />template directive parameters<br />debug parameter<br />System %TEMP% folder<br />language parameter<br />Values: C#, VB, C#3.5<br />inherits parameter<br />Tells which base class to use<br />hostspecific parameter<br />A reference to the object that the hosts the engine.<br />e.g. Host.TemplateFile<br />Host.StandardAssemblyReferences<br />
    13. 13. T4 Syntax<br />assembly directive<br /><#@ assembly #><br /> Parameter<br /> name<br /> could be an assembly in the GAC<br /><#@ assembly name=“System.Data” #><br /> absolute path to the assembly<br /><#@ assembly name=“c:projectsproj1bindebugproj1.dll” #><br />
    14. 14. T4 Syntax<br />import directive<br /> <#@ import #><br /> Parameter<br />namespace<br />E.g. <#@ import namespace=“System.Collections.Generic” #><br />
    15. 15. T4 Syntax<br />include directive<br /> <#@ include #><br /> Parameter<br /> file<br /> e.g. <#@ include file=“includedfile.tt” #><br />
    16. 16. Demo<br />directives<br />
    17. 17. T4 Syntax<br />On to T4 blocks<br />
    18. 18. T4 Syntax<br />T4 Blocks<br /> Text blocks<br /> Code blocks<br /> Statement<br /> Expression<br /> Class feature<br />
    19. 19. T4 Syntax<br />Text Blocks<br />Non-programmatic plain text<br />
    20. 20. T4 Syntax<br />Statement Blocks<br /> Delineated using<br /> <# #><br />
    21. 21. T4 Syntax<br />Expression Blocks<br /> Delineated using<br /> <#= #><br />
    22. 22. T4 Syntax<br />Class Feature Blocks<br /> Delineated using<br /> <#+ #><br /> Add any class level construct<br />Helper functions<br />Fields<br />Constants<br />Properties<br />
    23. 23. Demo<br />T4 Blocks<br />
    24. 24. Debugging T4 Templates<br />Debugging<br />Insert System.Diagnostics.Debugger.Launch(); anywhere in the code blocks<br />
    25. 25. Demo<br />Debugging T4 Templates<br />
    26. 26. Break ?<br />
    27. 27. Custom T4 Host using GAX<br />Custom T4 Host<br /> Guidance Automation Extensions<br />http://bit.ly/fW99Zh<br /> Add references in VS<br />Microsoft.Practices.RecipeFramework.VisualStudio.Library<br />Microsoft.VisualStudio.TextTemplating<br />
    28. 28. Demo<br />Custom T4 Host<br />
    29. 29. Demo<br />Compile Generated Code<br />
    30. 30. T4 Item Templates<br />VS T4 Item Templates<br /> Text Template<br />TextTemplatingFileGenerator<br />Preprocessed Text Template<br />TextTemplatingFilePreprocessor<br /> Redistributing T4 engine with your app<br />
    31. 31. Demo<br />T4 Item Templates<br />
    32. 32. VS 2010 Item/Project Templates<br />VS Item Templates<br />Single file versus multi-file templates<br />As .vsi/.zip<br />VS 2010<br />/{vs.net folder}/common7/ide/itemtemplates<br />/{user folder}Visual Studio 2010TemplatesItemTemplates<br />Template parameters<br />When the template wizard runs they are replaced<br />
    33. 33. VS 2010 Item/Project Templates<br />Parameters to a VS template file<br />Complete list of all parameters: http://bit.ly/hJayFX<br />
    34. 34. VS 2010 Item/Project Templates<br />VS Template files<br /> Single File Template<br /> Multi-File Template<br />
    35. 35. Demo<br />VS 2010 Item/Project Templates<br />
    36. 36. CTAs<br />Try out the Text Template item template<br />Start thinking of scenarios where you can apply Domain Specific Development<br />Look at other solutions that have used this development pattern<br />Many on Codeplex.com<br />
    37. 37. Resources<br />Where to go for resources?<br />Code Generation and Text Templates (T4)<br />http://bit.ly/eJL91t<br />T4 Templates: A Quick-Start Guide for ASP.NET MVC Developers<br />http://bit.ly/cVKYp5<br />Language Oriented Programming: The Next Programming Paradigm:<br />http://bit.ly/bVqYoG<br />CodePlex: http://bit.ly/i0VnHK<br />Book<br />Domain-Specific Development with Visual Studio DSL Tools<br />ISBN: 0321398203<br />
    38. 38. Visual Studio Plug-ins<br />Plug-in by Tangible Engineering<br />http://bit.ly/dfFhEi<br />T4 Editor by Clarius<br />http://bit.ly/cHfGRn<br />Resources<br />
    39. 39. Resources will be posted<br />Blog: http://www.codekindler.com<br />Email: joubin.najmaie@gmail.com<br />Resources<br />
    40. 40. T4 / Code Generation<br />Q&A<br />