0% found this document useful (0 votes)
323 views

Code Generation With t4 Text Templates Introduction

The document demonstrates how to use the Text Template Transformation Toolkit (T4) to generate text output from templates. It shows examples of basic text output, looping, code blocks, and field declarations in T4 templates and the generated output. Processing directives, template compilation, and potential errors are also discussed at a high level.

Uploaded by

vamosraghava
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
323 views

Code Generation With t4 Text Templates Introduction

The document demonstrates how to use the Text Template Transformation Toolkit (T4) to generate text output from templates. It shows examples of basic text output, looping, code blocks, and field declarations in T4 templates and the generated output. Processing directives, template compilation, and potential errors are also discussed at a high level.

Uploaded by

vamosraghava
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Oleg Sych www.olegsych.

com

SqlConnection connection = new SqlConnection( "Data Source=.;Initial Catalog=Northwind;Integrated Security=True"); connection.Open(); SqlCommand command = connection.CreateCommand(); command.CommandText = "select ContactName from Customers where City = @City"; command.Parameters.AddWithValue("@City", "London"); SqlDataReader londoners = command.ExecuteReader(); while (londoners.Read()) Console.WriteLine(londoners["ContactName"]);

NorthwindDataContext northwind = new NorthwindDataContext(); var londoners = from customer in northwind.Customers where customer.City == "London" select customer; foreach (var customer in londoners) Console.WriteLine(customer.ContactName);

Demo

Text Template Transformation Toolkit ASP.NET-like template syntax Generates any text files Supports Visual Basic and C# Built into Visual Studio 2008 Available as a download for Visual Studio 2005

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

Text Blocks

Code Blocks

Template

<#@ template language="C#" #> Hello <# Write("World!"); #> public class GeneratedTextTransformation: Microsoft.VisualStudio.TextTemplating.TextTransformation { public override string TransformText() { this.Write("Hello\r\n"); Write("World!"); return this.GenerationEnvironment.ToString(); } } Hello World!

Generate

Compiled Template

Run

Output

Template

<#@ template language="C#" #> <# for(int i = 1; i <= 3; i++) { #> Hello World! <# } #> namespace Microsoft.VisualStudio.TextTemplating25E14C49A0E { public class GeneratedTextTransformation: Microsoft.VisualStudio.TextTemplating.TextTransformation { public override string TransformText() { for (int i = 1; i <= 3; i++) { this.Write("Hello World!\r\n"); } return this.GenerationEnvironment.ToString(); } } } Hello World! Hello World! Hello World!

Generate

Compiled Template

Run

Output

Template

<#@ template language="C#" #> <# for(int i = 1; i <= 3; i++) { #> Hello World <#= i #>! <# } #> namespace Microsoft.VisualStudio.TextTemplating76E036E { public class GeneratedTextTransformation: Microsoft.VisualStudio.TextTemplating.TextTransformation { public override string TransformText() { for (int i = 1; i <= 3; i++) { Write("Hello World "); Write(ToStringHelper.ToStringWithCulture(i)); Write("!\r\n"); } return GenerationEnvironment.ToString(); } } } Hello World 1! Hello World 2! Hello World 3!

Generate

Compiled Template

Run

Output

Template

Generate

<#@ template language="C#"#> <# HelloWorld(); #> <#+ private void HelloWorld() { Write("Hello World"); } #> namespace Microsoft.VisualStudio.TextTemplatingD6871B481C9 { public class GeneratedTextTransformation: Microsoft.VisualStudio.TextTemplating.TextTransformation { public override string TransformText() { HelloWorld(); return GenerationEnvironment.ToString(); } private void HelloWorld() { Write("Hello World"); } } } Hello World

Compiled Template

Run

Output

Template

Generate

<#@ template language="C#" #> <# HelloWorld(); #> <#+ private string _field = "World"; private void HelloWorld() { #> Hello <#=_field#> <#+ } #> namespace Microsoft.VisualStudio.TextTemplatingD6871B481C9 { public class GeneratedTextTransformation: Microsoft.VisualStudio.TextTemplating.TextTransformation { public override string TransformText() { HelloWorld(); return GenerationEnvironment.ToString(); } private string _field = World"; private void HelloWorld() { this.Write("Hello "); this.Write(ToStringHelper.ToStringWithCulture(_field)); } } } Hello World

Compiled Template

Run

Output

Demo

<#@ template language="C#","VB","C#v3.5" or "VBv3.5" debug="False" or "True" inherits="TextTransformation" hostspecific="False" or "True" culture="","en-US"... #>

<#@ output extension="cs","vb","txt"... encoding="","ASCII","Unicode" #>

<#@ include file="RelativePath\File.tt" or "C:\AbsolutePath\File.tt" #>

<#@ assembly name="AssemblyName" or "C:\AbsolutePath\Assembly.dll #>

<#@ import namespace="System.Xml" #>

Template

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

Compilation Errors Generate


public class GeneratedTextTransformation: Microsoft.VisualStudio.TextTemplating.TextTransformation { public override string TransformText() { this.Write("Hello\r\n"); Write("World!"); return this.GenerationEnvironment.ToString(); } }

Compiled Template

Run

Runtime Errors Output


Hello World!

Demo

Improve Developer Productivity Improve Code Quality Improve Application Performance

Config files Decorators CRUD stored procedures Strongly-typed ASP.NET navigation classes Strongly-typed AzMan wrappers ADO.NET Entity Framework Stored Procedures ADO.NET Entity Framework Views LINQ to SQL Data Context WiX source files SQL views from enumerations

Visual Studio SDK


Domain-Specific Language Tools

www.olegsych.com
Presentations
Code Generation with T4 Text Templates

Articles
Text Template Transformation Toolkit T4 Architecture T4 Template Design

www.t4editor.net http://www.codeplex.com/t4toolbox

You might also like