Code Generation using T4<br />Joubin Najmaie<br />Software Architect @ Softchoice<br />Twitter: @joubin<br />Personal blog...
T4 - Agenda<br />The “suit” story<br />Demo<br />T4 Overview<br />T4 Syntax<br />Demo<br />More demos<br />
A “suit” calls a meeting<br />Empower the business users<br />Give control over                              their content...
Run back to your desk<br />
Site Content analysis<br />
Architecting a solution<br />Define content type descriptions etc.<br />
You start coding away<br />
Soon you’ll realize …<br />Every time you add a content type<br />Data Access Layer code<br />
Introducing T4<br />
What T4 is not?<br />
What is T4?<br />T4 is a vehicle<br />Generates code from described models<br />Models could be<br />Visual<br />Class dia...
What is T4? (cont.)<br />A set of tools to build other tools<br />Describe<br />Domain Model<br />User interface<br />Busi...
T4 - the what<br />What does it stand for?<br />What are the Inputs?<br />What are the outputs?<br />What does the syntax ...
Current usages of T4 in VS.NET<br />ASP.NET MVC Views<br />Entity Framework 4 model generation<br />VS Project templates<b...
T4 - Syntax<br />A quick overview of T4 syntax<br />Lots of demos<br />
Anatomy of T4 templates<br />
Text Templating<br />Write a text template<br />T4 engine generates a compiled intermediary class<br />T4 engine executes ...
Text Templating<br />
Processing Directives<br />Similar to ASPX and ASCX<br /><#@ and end with #><br />Directives<br />template<br />assembly<b...
template directive<br /><#@ template debug="true" language="C#" #><br />Parameters<br />debug<br />language<br />inherits<...
template directive parameters<br />debug parameter<br />System %TEMP% folder<br />language parameter<br />Values: C#, VB, ...
assembly directive<br /><#@ assembly #><br />Parameter<br />name<br />could be an assembly in the GAC<br /><#@ assembly na...
import directive<br /><#@ import #><br />Parameter<br />namespace<br /><#@ import      namespace=“System.Collections.Gener...
include directive<br /><#@ include #><br />Parameter<br />file<br /><#@ include file=“includefile.tt” #><br />
T4 Blocks<br />Text blocks<br />Code blocks<br />Statement blocks<br />Expression blocks<br />Class feature blocks<br />
Text blocks<br />Non-programmatic text<br />
Text blocks - Syntax<br />
Statement blocks<br />Delineated using<br /><# #><br />
Statement blocks - Syntax<br />
Expression blocks<br />Delineated using<br /><#= #><br />
Expression blocks - Syntax<br />
Class feature blocks<br /><#+ #><br />Used to add any class level construct<br />Helper functions<br />Fields<br />Constan...
Class feature blocks - Syntax<br />
Custom Text Template Host<br />When would you write one?<br />Want to run T4 templates outside of VS.NET<br />Integrate fu...
Code generation<br />
VS.NET Item template<br />Single file versus multi-file templates<br />As .vsi/.zip<br />VS 2010<br />/{vs.net folder}/com...
Parameters<br />
Multi-file item template<br />
T4 editors in VS.NET<br />Plug-in by Tangible Engineering *<br />http://visualstudiogallery.msdn.microsoft.com/en-us/60297...
Slides will be on blog<br />http://joubin.ca<br />CodePlex project<br />http://codekindler.codeplex.com<br />Provides boil...
Upcoming SlideShare
Loading in …5
×

Code Generation using T4

3,164 views

Published on

Text Template Transformation Toolkit

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,164
On SlideShare
0
From Embeds
0
Number of Embeds
42
Actions
Shares
0
Downloads
0
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Code Generation using T4

  1. 1. Code Generation using T4<br />Joubin Najmaie<br />Software Architect @ Softchoice<br />Twitter: @joubin<br />Personal blog: http://joubin.ca<br />http://www.softchoice.com<br />
  2. 2. T4 - Agenda<br />The “suit” story<br />Demo<br />T4 Overview<br />T4 Syntax<br />Demo<br />More demos<br />
  3. 3. A “suit” calls a meeting<br />Empower the business users<br />Give control over their content<br />
  4. 4. Run back to your desk<br />
  5. 5. Site Content analysis<br />
  6. 6. Architecting a solution<br />Define content type descriptions etc.<br />
  7. 7. You start coding away<br />
  8. 8. Soon you’ll realize …<br />Every time you add a content type<br />Data Access Layer code<br />
  9. 9. Introducing T4<br />
  10. 10. What T4 is not?<br />
  11. 11. What is T4?<br />T4 is a vehicle<br />Generates code from described models<br />Models could be<br />Visual<br />Class diagrams<br />ERDs<br />Text-based<br />XSD<br />Database<br />Metadata<br />
  12. 12. What is T4? (cont.)<br />A set of tools to build other tools<br />Describe<br />Domain Model<br />User interface<br />Business process<br />Flow<br />Generate code from model<br />Use T4 to generate code<br />
  13. 13. T4 - the what<br />What does it stand for?<br />What are the Inputs?<br />What are the outputs?<br />What does the syntax look like?<br />What are the supported languages?<br />
  14. 14. Current usages of T4 in VS.NET<br />ASP.NET MVC Views<br />Entity Framework 4 model generation<br />VS Project templates<br />VS Item templates<br />Web forms<br />Class<br />
  15. 15. T4 - Syntax<br />A quick overview of T4 syntax<br />Lots of demos<br />
  16. 16. Anatomy of T4 templates<br />
  17. 17. Text Templating<br />Write a text template<br />T4 engine generates a compiled intermediary class<br />T4 engine executes the compiled class to generate output<br />
  18. 18. Text Templating<br />
  19. 19. Processing Directives<br />Similar to ASPX and ASCX<br /><#@ and end with #><br />Directives<br />template<br />assembly<br />import<br />output<br />include<br />
  20. 20. template directive<br /><#@ template debug="true" language="C#" #><br />Parameters<br />debug<br />language<br />inherits<br />hostspecific<br />
  21. 21. 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 />
  22. 22. assembly directive<br /><#@ assembly #><br />Parameter<br />name<br />could be an assembly in the GAC<br /><#@ assembly name=“System.Data” #><br />or absolute path to the assembly<br /><#@ assembly name=“c:projectsproj1indebugproj1.dll” #><br />
  23. 23. import directive<br /><#@ import #><br />Parameter<br />namespace<br /><#@ import namespace=“System.Collections.Generic” #><br />
  24. 24. include directive<br /><#@ include #><br />Parameter<br />file<br /><#@ include file=“includefile.tt” #><br />
  25. 25. T4 Blocks<br />Text blocks<br />Code blocks<br />Statement blocks<br />Expression blocks<br />Class feature blocks<br />
  26. 26. Text blocks<br />Non-programmatic text<br />
  27. 27. Text blocks - Syntax<br />
  28. 28. Statement blocks<br />Delineated using<br /><# #><br />
  29. 29. Statement blocks - Syntax<br />
  30. 30. Expression blocks<br />Delineated using<br /><#= #><br />
  31. 31. Expression blocks - Syntax<br />
  32. 32. Class feature blocks<br /><#+ #><br />Used to add any class level construct<br />Helper functions<br />Fields<br />Constants<br />Properties<br />
  33. 33. Class feature blocks - Syntax<br />
  34. 34. Custom Text Template Host<br />When would you write one?<br />Want to run T4 templates outside of VS.NET<br />Integrate functionality into custom tools<br />
  35. 35. Code generation<br />
  36. 36. VS.NET Item template<br />Single file versus multi-file templates<br />As .vsi/.zip<br />VS 2010<br />/{vs.net folder}/common7/ide/itemtemplates<br />Template parameters are special params<br />When the template wizard runs they are replaced.<br />
  37. 37. Parameters<br />
  38. 38. Multi-file item template<br />
  39. 39. T4 editors in VS.NET<br />Plug-in by Tangible Engineering *<br />http://visualstudiogallery.msdn.microsoft.com/en-us/60297607-5fd4-4da4-97e1-3715e90c1a23<br />T4 Editor by Clarius<br />http://www.visualt4.com/downloads.html<br />
  40. 40. Slides will be on blog<br />http://joubin.ca<br />CodePlex project<br />http://codekindler.codeplex.com<br />Provides boilerplate classes using T4<br />Only a few at the moment<br />Thank You!<br />
  41. 41. Thanks to our contributors<br />

×