BT

Using T4 in ASP.NET MVC

by Abel Avram on Feb 10, 2009 |

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.

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Educational Content

General Feedback
Bugs
Advertising
Editorial
InfoQ.com and all content copyright © 2006-2014 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT