InfoQ

InfoQ

News

My Bookmarks

Login or Register to enable bookmarks for unlimited time.

The content has been bookmarked!

There was an error bookmarking this content! Please retry.

Text Template Transformation Toolkit in Visual Studio 2008

Posted by Abel Avram on Apr 17, 2008

Sections
Development,
Architecture & Design
Topics
.NET ,
Domain Specific Languages ,
Artifacts & Tools
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
Re: Example of Text Template Transformations in action ... by Francois Ward Posted
Re: Example of Text Template Transformations in action ... by Billy McCafferty Posted
  1. Back to top

    Example of Text Template Transformations in action ...

    by Graham Sibley

    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. Back to top

    Re: Example of Text Template Transformations in action ...

    by Francois Ward

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

  3. Back to top

    Re: Example of Text Template Transformations in action ...

    by Billy McCafferty

    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

Attila Szegedi on JVM and GC Performance Tuning at Twitter

Attila Szegedi talks about performance tuning Java and Scala programs at Twitter: how to approach GC problems, the importance of asynchronous I/O, when to use MySQL/Cassandra/Redis, and much more.

10 tips on how to prevent business value risk

One category of risk that project teams need to ensure they address is business value failure – delivering a product that fails to provide value for the business investor.

Interview: Software Systems Architecture: Working With Stakeholders Using Viewpoints and Perspectives

InfoQ spoke to the authors of Software Systems Architecture on a couple of new topics, the System Context viewpoint and Agile, which have been added to the second edition.

Beauty Is in the Eye of the Beholder

Alex Papadimoulis discusses ugly code, where it comes from, how to avoid it, and how to get rid of it.

Architecting Visa for Massive Scale and Continuous Innovation

John Davies examines Visa’s architecture and shows how enterprises have architected complex integrations incorporating Hadoop, memcached, Ruby on Rails, and others to deliver innovative solutions.

Max Protect: Scalability and Caching at ESPN.com

Sean Comerford unveils ESPN.com’s architecture, what components are used and why, and the current changes the website goes through.

The Seven Deadly Sins of Enterprise Agile Adoption

Are there repeated patterns of failure on Enterprise Agile Enablement efforts? Sanjiv and Arlen discuss Seven Deadly Sins to avoid when adopting Agile in an enterprise.

Questions for an Enterprise Architect

Erik Dörnenburg answers: What is Enterprise and Evolutionary Architecture?, discussing 4 issues: Turning strategy into execution, Ensuring conformance, Where do the architects sit? Buying or building?