Twin Cities Code Camp

Jon Stonecash

Tips and Tricks for Code Generation with T4

by Jon Stonecash

Fri, Oct 01, 2010
Room:
Time: 0:00

The recent versions of Microsoft Visual Studio use code generation to support a number of features, using the T4 code generator that is built into each copy of visual studio. T4 stands for the Text Template Transformation Toolkit and with some work is also available to the software developer. This session is a compilation of practical tips and tricks for using T4 to generate everything from snippets of code to entire data access layers:

  • The syntax and semantics of the template language.
  • The inclusion of sub-templates
  • The process for debugging templates (or "why you do not want to put any more code than you have to into the template)
  • The process of removing logic from the templates by writing helper assemblies.
  • The process of removing logic from the templates by writing pre-processor packages.
  • Helpful add-ins to make editing template code less painful.
  • The reasons for using partial classes and even partial methods.
  • The reasons for running code generation in a project other than where the generated code will finally live.

T4! Just do it!


About the Author