T4 Template Design: Standalone Template


Standalone Template is one of T4 template design techniques that can be used to develop reusable templates. The main defines parameters as properties that retrieve their values from an external data store, such as CallContext or an XML file. The calling template uses T4 engine to compile the standalone template into a separate assembly and run the compiled code independently of the calling template.

Main Template

<#@ template language="C#" #>
<#@ import namespace="System.Runtime.Remoting.Messaging" #>
public class <#= ClassName #>
{
    public string Property
    {
        get { return _property; }
        set { _property = value; }
    }
}
<#+
    string ClassName
    {
        get { return (string)CallContext.LogicalGetData("ClassName"); }
    }
#>

Calling Template

<#@ template language="C#" hostspecific="True" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Runtime.Remoting.Messaging" #>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #>
<#
  CallContext.LogicalSetData("ClassName", "TestClass");
  string output = ProcessTemplate("Template.tt");
  Write(output);
#>
<#+
  string ProcessTemplate(string templateFileName)
  {
    string template = File.ReadAllText(Host.ResolvePath(templateFileName));
    Engine engine = new Engine();
    return engine.ProcessTemplate(template, Host);
  }
#>

Output

public class TestClass
{
    public string Property
    {
        get { return _property; }
        set { _property = value; }
    }
}

How It Works

The main template defines its parameters as read-only properties that get their values from the remoting CallContext, such as ClassName property in the example above. The calling template provides parameter values by placing them in the CallContext. It then uses T4 Engine to compile and execute the main template and save its output.

Pros

Standalone Template technique allows to parameterize a one-off template and reuse it to generate multiple artifacts of the same type. This technique provides good encapsulation of the main template and allows to compose templates to generate multiple outputs from a single calling template.

This technique can be used to invoke a T4 template from external code, such as a console application.

Cons

Standalone Template technique requires using an external, weakly-typed mechanism, such as CallContext or an XML file, to pass parameter values to the main template.

This technique does not allow to extend the main template without resorting to modification of the main template itself.

About T4

T4 (Text Template Transformation Toolkit) is a template-based code generation engine. It is available in Visual Studio 2008 and as a download in DSL and GAT toolkits for Visual Studio 2005. T4 engine allows you to use ASP.NET-like template syntax to generate C#, T-SQL, XML or any other text files.

For more information about T4, check out my previous article.

Information and Links

Join the fray by commenting, tracking what others have to say, or linking to it from your blog.


Other Posts
T4 Template Design: Nested Template Class
T4 Template Design: Template Method

Write a Comment

Take a moment to comment and tell us what you think. Some basic HTML is allowed for formatting.

Reader Comments

Be the first to leave a comment!