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

Confessions of A New Agile Developer

This short article is a first-person case history of someone taking up Agility for the first time. It covers the problems and reactions that are common to most teams and most developers.

Scott Chacon on Git and GitHub

Scott Chacon talks about the technologies that power GitHub (Erlang, Redis,...), and the benefits of Git as a version control and as a storage system. Also: ShowOff, a JS-based presentation tool.

Reformulating the Product Delivery Process

Israel Gat, Erik Huddleston and Stephen Chin present how Inovis realized a higher product throughput by using three unconventional Kanban practices and a Lean Release Management tool called APROPOS.

Enterprise Mashups: Why Do I Care?

Ross Mason discusses how to use enterprise mashups by applying a number of patterns, such as FeedFactory, Super Search, and Pipeline, in order to find new ways to benefit from existing enterprise data

Command-Query Responsibility Segregation

Udi Dahan discusses the Command Query Responsibility Segregation (CQRS) pattern, detailing on queries and commands, what they are and how they should be used in an asynchronous programming environment

The Story of a Project

Olivier Mallassi shares a story of a typical software development project, some typical problems and what he learned from Tom Demarco about addressing those problems, and an alternative story.

Living and Working with Aging Software

Ralph Johnson discusses principles, practices and tools relating to software development starting from already existing code which needs refactoring, maintenance, and sometimes architectural change.

IIBA Debate on the Role of the Analyst in Agile Projects

At a recent IIBA New Zealand members event Shane and Pete debated the role of the business analyst on Agile projects. They looked at the importance of analysis on projects and how the role changes.