InfoQ

News

Text Template Transformation Toolkit in Visual Studio 2008

Posted by Abel Avram on Apr 17, 2008

Community
.NET
Topics
Artifacts & Tools ,
Domain Specific Languages
Tags
Visual Studio

Visual Studio 2008 includes a Text Template Transformation Toolkit, also known as T4, as part of Domain-Specific Language Tools, and can be used to generate code based on a text template. T4 can be also used with Visual Studio 2005 being included in the DSL and GAT toolkits.

According to MSDN:

A text template is a file that contains a mixture of text blocks and control logic. When you transform a text template, the control logic combines the text blocks with the data in a model to produce an output file. You can use text templates to create text artifacts such as code files and HTML reports. For example, a model of the flow of control between the individual pages in a user interface, such as a wizard, can be passed as input to a text template. The text template can generate code and configuration files that implement that flow of control.

A text template undergoes a transformation process which takes the text template file as input, transforms it, and generates an output text file. The transformation process has two steps. In the first step, the T4 engine uses the text template to generate a temporary transformation class. In the second step, the T4 engine compiles the transformation class into an assembly and executes the code, generating an output text file.

The "Hello World!" example looks like this:

<#@ template language="C#" #>
<# Write("Hello World!") #>

The text template can contain Visual Basic or C# code, C# being the default language if one is not specified. The output directive is used to specify the generated text file type. There can be many output file types, some of the most usual ones being .html, .txt, .cs, .vb. The default output file type is .cs. Following is an example on how to use the output directive:

<#@ output extension=".html" #>

There is limited support in Visual Studio for editing text templates. Clarius Consulting has a free editor "offering support for colorization of the different T4 template parts, IntelliSense, and design-time preview of templates". Visual Studio supports debugging of text templates and of the transformation process, but it is not straightforward. Detailed explanations on debugging can be found on MSDN, while Gareth Jones has a short tutorial. Oleg Sych has posted an introduction article on T4. The more comprehensive MSDN documentation is available here.

Example of Text Template Transformations in action ... by Graham Sibley Posted Apr 17, 2008 8:45 AM
Re: Example of Text Template Transformations in action ... by Francois Ward Posted Apr 17, 2008 1:53 PM
Re: Example of Text Template Transformations in action ... by Billy McCafferty Posted Feb 12, 2009 2:35 PM
  1. My company recently released a new product that leverages Text Template Transformations as part of a business-driven modeling and code generation platform that integrates with Visual Studio .Net. The product is called Viewpoints and you can check it (along with some T4 samples) out at:
    www.gnomzsoftware.com/viewpoints/.

  2. That looks pretty nice... That entity designer is quite pretty too. You guys really had fun with the DSL extension toolkit :)

  3. If you'd like some real world T4 taken just about as far as it'll go, I'd invite you to take a look at the Crud scaffolding capabilities found within S#arp Architecture (code.google.com/p/sharp-architecture/). There's plenty of direction in the documentation found within the beta release.

    Billy McCafferty

Educational Content

Brian Marick on 4 Challenges and 5 Guiding Values of Agile Software Development

Brian Marick takes us through a quick tour of the most important values and challenges to adopting Agile successfully (they aren't the typical challenges and values we hear in the community).

Are You a Software Architect?

The line between development and architecture is tricky. Does it exist at all? Is an ivory tower actually needed? There's a balance in the middle, but how do you move from developer to architect?

Agile – A Way of Life and Pragmatic Use of Authority

The word 'authority' sometimes produces an allergic response in hard-line agilists. Freedom and authority – both are bad if misused and both are good if used in right spirit for a noble cause.

Getting Started with Grails, Second Edition

"Getting Started with Grails" brings you up to speed on this modern web framework. Companies as varied as LinkedIn, Wired, and Taco Bell are all using Grails. Are you ready to get started as well?

Using ITIL V3 as a Foundation for SOA Governance

Those familiar with only ITIL V2 often scoff at the thought that ITIL could serve as a governance framework for SOA. With ITIL V3, the focus of the framework shifted towards service-orientation.

Adrian Colyer on AspectJ, tc Server and dm Server

SpringSource CTO Adrian Colyer discusses AspectJ, SpringSource's dm Server and tc Server products, OSGi and Scrum.

Adam Wiggins on Heroku

Heroku's Adam Wiggins talks about Rails, Background Jobs, Add-Ons, Ruby, and how Heroku manages to work around Ruby's inefficiencies using Erlang and other languages.

SOA as an Architectural Pattern: Best Practices in Software Architecture

For Grady Booch the foundation of a good architecture is patterns, SOA being just one of many patterns. In this Second Life presentation, Booch attempts to bring more clarity on what architecture is.