Chris Melinn
Agenda
This presentation will provide a
short overview of T4. We will learn
the basics of creating a template and
then see a few examples of how T4 is
used by others.
The Basics
In the Wild
More Information
What is T4?
T4 =
Text
Template
Transformation
Toolkit
Code Generation
Built by Microsoft
Included in Visual Studio
What is T4?
Generates any textual format
Common examples:
• Code (C#, VB, etc)
• SQL
• XML
Template Based
Similar syntax to ASP
Very Extensible
But…
Editing T4 sucks *
* with the default Visual Studio setup
No Syntax Highlighting
No Intellisense
Looks like Notepad…
The DefaultVisual Studio Editor
Fortunately, there is a solution…
Free Visual T4 Editor
Community Edition
from Clarius Consulting
Much better experience
Syntax Highlighting
Intellisense in Pro Edition
The Visual T4 Editor (inside Visual Studio)
Creating Templates
How it works
T4 engine performs two steps to generate output from a template:
Source: Oleg Sych http://www.olegsych.com/
Examples of T4 in the wild
“If you’re going to do something twice or more, manually, in your company, generate it.”
~ Scott Hanselman
T4 can be used to generate…
Code Templates
(MVC Add View)
Database to Code
Code to Database
Configuration Files
Domain-Specific Languages
(DSL Toolkit)
Removing
Magic Strings
(T4MVC)
Add logic to CSS
(T4CSS)
ASP.NET MVC CodeTemplates
T4CSS
For more information…
Resources
Oleg Sych’s blog is the best collection of T4 resources I have found anywhere
http://www.olegsych.com/
His CodePlex project, T4 Toolbox, contains several ready-to-use code generation templates and other supporting extensions.
http://www.codeplex.com/t4toolbox
Editing Tools
Visual T4 Editor from Clarius Consulting
http://www.visualt4.com/
Tangible T4 Editor by Tangible Engineering
http://t4-editor.tangible-engineering.com/T4-Editor-Visual-T4-Editing.html
Other Examples of T4
T4CSS
http://haacked.com/archive/2009/12/02/t4-template-for-less-css.aspx
T4MVC: Creates strongly typed helpers to eliminate your “magic strings”
http://aspnet.codeplex.com/wikipage?title=T4MVC
Generate WPF and Silverlight Dependency Properties using T4
http://www.codeproject.com/KB/WPF/DependencyPropertyCodeGen.aspx
Contact Me
Blog: http://chrismelinn.wordpress.com
Twitter: http://twitter.com/ChrisMelinn