Code Generation With t4 Text Templates Introduction
Code Generation With t4 Text Templates Introduction
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"... #>
Template
Compiled Template
Run
Demo
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
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