Using T4 in ASP.NET MVC
ASP.NET MVC is using T4 (Text Template Transformation Toolkit) to generate the code behind the scenes when a Controller or a View is added to a project. T4 is a fully customizable text generator based on templates.
One of the ASP.NET MVC features announced by Scott Guthrie is using T4 for code generation. The code is generated by the T4 engine from a text template. That means anyone can create or edit such a template having complete control over the resulting code.
A T4 template looks very similar to any other Web Form combining plain text blocks with control logic. Abhishek Mishra gave a detailed example of editing one.
There is no intellisense support in Visual Studio for editing T4 templates, but Clarius Consulting offers a T4 Editor Community Edition integrated with VS and providing syntax highlighting. They also have a Pro version offering true intellisense support and T4 Code Generator edition supporting custom T4 hosts (WSSF, ASP.NET MVC), Server Explorer integration, Drag & Drop XML and XSD files, Friendly DB API to inspect metadata, and others.
T4 can be used to automate the generation of text files of any type and purpose. Scott Hanselman gave such an example by using a template for generating LINQ to SQL code. Scott recommends using T4 for any repetitive text generation task, not just .NET related: “If you're doing something twice or more, manually, in your company, generate it.”
InfoQ provides a technical introduction to T4 including links to MSDN documentation and useful posts.
Ruslan Meshenberg Sep 21, 2014