Chapter 4. Transforming Text Templates

In This Chapter

  • Discovering where templating for Visual Studio originated

  • Figuring out when to use T4

  • Getting your environment ready for T4 use

  • Using some basic T4 directives

T4 (Text Template Transformation Toolkit) is a code generation toolkit built into Visual Studio. It is a built in language used to make the default ASP.NET and Windows Forms files in C# or VB, for example, when you use the Add New Item feature.

Scott Hanselman, from Microsoft, said it best: "Now's the time to introduce code generation to your company." I wholeheartedly agree. Code generation increases developer productivity, decreases bugs in final versions, and makes people happy.

Lars Corneliussen, a Microsoft MVP, said, "T4 in VS is the equivalent to index.php in Apache," and he isn't far from the truth. T4 is a starting point for a project, providing seamless redirection and logical formatting of what the user (in this case, the developer) actually sees. It is the template, just as the name suggests.

T4 is a big topic, like so many others in this book, but you should be able to get started creating code generation from the material here. Getting your environment ready and understanding the concepts are the most important steps.

Getting to Know T4

In this chapter, I look at T4 as a code-generation tool. It is a lot more than that. Really, it is a tool for the management of metadata around Domain Specific Languages. If that makes no sense to you, join the club.

Many things about ...

Get C# 2010 All-in-One For Dummies® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.