I've long been a fan of CodeSmith, an easy-to-use templating engine for creating code of any kind, though I've been a fan from the sidelines, never having purchased a license. My code generation needs are simply too infrequent to justify myself or my company purchasing a commercial code generator. As a result, I've resorted to creating ugly Console projects to generate code on the rare occasions I don't want to do it by hand.
Not anymore.
Microsoft has quietly included a new template-based code generation tool in Visual Studio 2008 called the Text Template Transformation Toolkit, a typical Microsoft-mouthful product name which is often shortened to T4. Officially part of Microsoft's DSL Tools project, T4 is actually a tool that any developer can leverage whether they're working with DSL tools or not. You won't find T4 advertised anywhere inside Visual Studio, but if you create a text file in your project with the extension ".tt" you'll see the T4 magic start to kick in: an associated code file is nested under the new ".tt" file, and Visual Studio understands the ".tt" file as an ASP.NET-like "text template" which it uses to generate the associated code file for your project. You can then develop repetitive code via the template, get feedback on whether the template compiles or not, and watch your associated code file become part of the build process.
Out-of-the-box editing of text templates is pretty poor though: there's no IntelliSense, and there's no syntax highlighting, which is most-likely why Microsoft has made this a "hidden" feature. The good news is there is a free T4 editor available from Clarius Consulting that eases some of the pain. Though it does not (yet) add IntelliSense support, it does add some much-needed code-block coloring so you can easily distinguish between what is template code and what is generated code. If you want to take T4 for a spin, do yourself a favor and go download the Claruis T4 Editor first.
Getting Started
Oleg Sych has written extensively about T4 and he has a great introductory article available on his blog:
http://www.olegsych.com/2007/12/text-template-transformation-toolkit/
If you want to learn about T4, Oleg's blog is one of the best places to go.
Limitations and Advice
Here are a few issues I've encountered when using text templates:
- C# extension method syntax is not recognized. You can still call extension methods, but you will need to call them as a normal static method from the class in which they are defined. For example, if you have a class called StringExtensions that defines an extension method called TrimToNull(), instead of calling myString.TrimToNull(), you'll have to call the static method directly: StringExtensions.TrimToNull(myString).
- Referencing your own assemblies to use within your template code takes just a little work. You either need to provide the full path to your assembly using a template assembly directive, or you need to add this path to your project's reference paths in the project properties dialog. Oleg explains more about this here: http://www.olegsych.com/2008/02/t4-assembly-directive/
- Don't make your template code depend on the assembly it is generating code for. This type of circular dependency is just begging for trouble. I foolishly headed down this route since I was generating code for a commonly-used utility library and I wanted to leverage that same library in my templates. If you really need to use some functionality from the assembly you're generating code for in your templates, either refactor the assembly into separate assemblies so there is no more circular dependency or save a recent build of the assembly into a different location and use that build exclusively for your templates.
- No IntelliSense. This is just part of life if you're going to use T4 right now. You can always keep an extra .cs or .vb file open for those times you want IntelliSense, and then copy and paste your code into the template. It's not the best, but it sure beats writing a Console project to generate code.
If Microsoft continues to improve T4 with a real editor that includes syntax highlighting, IntelliSense, and full support for C# and VB.NET language features such as extension methods, we'll have a real winner.