Click here to Skip to main content
15,881,809 members
Articles / Programming Languages / C#

T4 Lessons Learned

Rate me:
Please Sign up or sign in to vote.
4.00/5 (3 votes)
5 Oct 2012CPOL2 min read 11.3K   7   1
Valuable lessons learned in T4

Working with T4 recently, I learned a couple of valuable lessons I’d like to share.

Lesson 1: Template Language

One of the parameters of the language directive is the language, which specifies the language used in the code blocks of the template. Two languages are specified, C# and VB.

<#@ template language="C#" #>

However, any attempts to use var, lambdas, initialization lists or even inheritance failed. And then, I found the answer to the problem on Oleg Sych’s great blog. In VS2008, you can specify which version of the language you want to use. For instance, v3.5 for using C#/VB.NET 3.5.

<#@ template language="C#v3.5" #>

Then, suddenly, all of these missing features worked!

Lesson 2: Generate Multiple Output Files

There are cases when you want a T4 template to generate, not one, but several output files. And of course, you want them automatically added to the project, source control, all menu. For instance, maybe you want to generate files for a database object model. You have a single template that parses the description of the model and generates code, but the code should be put in different files.

Here is a dummy example, that produces five EntityX classes, all on the same file.

<# for(int i = 1; i <= 5; ++i)
   {
#>
      public class Entity<#=i#>
      {
      }
<#     
   }
#>

However, with the help of this script by Damien Guard, you can define blocks that get generated in different files. You can also define common blocks, such as header and footer for all output files. What you have to do is:

  • include the script into your template
  • create an instance of the blocks manager
  • define file blocks
  • optionally define header and footer blocks
  • process the blocks at the end of the template

Here is a concrete example for the above example:

<#@ template language="C#v3.5" hostspecific="True" #>
<#@include file="MultipleOutputHelper.ttinclude" #>
<# var manager = Manager.Create(Host, GenerationEnvironment); #>
<# manager.StartHeader(); #>
// Code generated by a template
using System;

namespace Tests
{
<# manager.EndBlock(); #>

<# for(int i = 1; i <= 5; ++i)
   {
      manager.StartNewFile(String.Format("Entity{0}.cs", i)); 
#>
      public class Entity<#=i#>
      {
      }
<#     
      manager.EndBlock();
   }
#>

<# manager.StartFooter(); #>
}
<# manager.EndBlock(); #>

<# manager.Process(true); #>

The result is five EntityX classes are generated, each in a separate file called EntityX.cs, and all these files are automatically added to the project.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Visma Software
Romania Romania
Marius Bancila is the author of Modern C++ Programming Cookbook and The Modern C++ Challenge. He has been a Microsoft MVP since 2006, initially for VC++ and nowadays for Development technologies. He works as a system architect for Visma, a Norwegian-based company. He works with various technologies, both managed and unmanaged, for desktop, cloud, and mobile, mainly developing with VC++ and VC#. He keeps a blog at http://www.mariusbancila.ro/blog, focused on Windows programming. You can follow Marius on Twitter at @mariusbancila.

Comments and Discussions

 
SuggestionI'm using this template Pin
André Ziegler8-Oct-12 8:27
André Ziegler8-Oct-12 8:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.