T4 / Code Generation
-
Upload
joubin-najmaie -
Category
Technology
-
view
113 -
download
1
description
Transcript of T4 / Code Generation
T4 / Code Generation
Presented by Joubin Najmaie (@codekindler)
February 23, 2011
Kitchener .NET UG: @cttdnug
Agenda Who am I? Motivations Behind Using T4 T4 Syntax Debugging T4 Templates Break Custom T4 Host using GAX Compiling Generated Code T4 Item Templates VS 2010 Item/Project Templates Resources Q&A
Who am I? Technical Architect at one of the largest IT solution
companies in North America Project: www.pagefragments.com Blog: www.codekindler.com Twitter: @codekindler Email: [email protected]
Who am I?
What is T4? Text Template Transformation Toolkit Transformation engine Vehicle to Generates code from described models Models could be
Visual Class diagrams ERDs, Entity Framework
Text-based XSD
Metadata
Motivations Behind Using T4
Why T4 is important? Developer guidance + speed Not limited to code: any text-based asset
Documentation generation Scaffolding applications
ORMs Domain Specific Development Extending Visual Studio
Item Templates Including latest ASP.NET MVC Controller and View templates
Project Templates
Motivations behind using T4
How I got to work with T4 Consultancy work – worked on a PoC migration of a static
site to a content managed with 1000+ pages Worked with a couple of CMSs – SharePoint, Sitecore and
an in-house CMS at an agency firm Patterns started emerging
Create fragments of a site that are most needed to be managed more regularly than others by different owners
The fragments in the system are compiled and included using T4 Addressing shortcomings and pains of CMSs
Motivations Behind Using T4
T4 Syntax
Anatomy of T4 templates
T4 Syntax
Text Templating ‘workflow’
T4 Syntax
Processing Directives
T4 Syntax
Similar to ASPX or ASCX <#@ and end with #>
Directives template assembly import output include
template directive
T4 Syntax
<#@ template debug="true" language="C#" #>
Parametersdebug language inheritshostspecific
template directive parameters
T4 Syntax
debug parameterSystem %TEMP% folder
language parameterValues: C#, VB, C#3.5
inherits parameterTells which base class to use
hostspecific parameterA reference to the object that the hosts the
engine.e.g. Host.TemplateFile Host.StandardAssemblyReferences
assembly directive
T4 Syntax
<#@ assembly #> Parameter
name could be an assembly in the GAC
<#@ assembly name=“System.Data” #>
absolute path to the assembly <#@ assembly name=“c:\projects\proj1\bin\debug\
proj1.dll” #>
import directive
T4 Syntax
<#@ import #> Parameter
namespaceE.g. <#@ import
namespace=“System.Collections.Generic” #>
include directive
T4 Syntax
<#@ include #> Parameter
file e.g. <#@ include file=“includedfile.tt” #>
Demodirectives
On to T4 blocks
T4 Syntax
T4 Blocks
T4 Syntax
Text blocks Code blocks
Statement Expression Class feature
Text Blocks
T4 Syntax
Non-programmatic plain text
Statement Blocks
T4 Syntax
Delineated using <# #>
Expression Blocks
T4 Syntax
Delineated using <#= #>
Class Feature Blocks
T4 Syntax
Delineated using <#+ #>
Add any class level constructHelper functionsFieldsConstantsProperties
DemoT4 Blocks
Debugging
Debugging T4 Templates
Insert System.Diagnostics.Debugger.Launch(); anywhere in the code blocks
DemoDebugging T4 Templates
Break ?
Custom T4 Host
Custom T4 Host using GAX
Guidance Automation Extensions http://bit.ly/fW99Zh
Add references in VS Microsoft.Practices.RecipeFramework.VisualStudio.Library Microsoft.VisualStudio.TextTemplating
DemoCustom T4 Host
DemoCompile Generated Code
VS T4 Item Templates
T4 Item Templates
Text Template TextTemplatingFileGenerator
Preprocessed Text Template TextTemplatingFilePreprocessor Redistributing T4 engine with your app
DemoT4 Item Templates
VS Item Templates
VS 2010 Item/Project Templates
Single file versus multi-file templatesAs .vsi/.zipVS 2010
/{vs.net folder}/common7/ide/itemtemplates /{user folder}\Visual Studio 2010\Templates\ItemTemplates
Template parametersWhen the template wizard runs they are
replaced
Parameters to a VS template file
VS 2010 Item/Project Templates
Tag name Description
$itemname$ Target file name
$safeitemname$Same as $itemname$ with all unsafe characters replaced
$fileinputname$ The file name provided by user
$rootnamespace$namespace of item at level of class
$machinename$ Computer name
$time$Current datetime in DD/MM/YYYY HH:MM:SS format
$username$ Current logged-in user
Complete list of all parameters: http://bit.ly/hJayFX
VS Template files
VS 2010 Item/Project Templates
Single File Template Multi-File Template
DemoVS 2010 Item/Project Templates
CTAs Try out the Text Template item template Start thinking of scenarios where you can apply Domain
Specific Development Look at other solutions that have used this development
pattern Many on Codeplex.com
Where to go for resources? Code Generation and Text Templates (T4)
http://bit.ly/eJL91t T4 Templates: A Quick-Start Guide for ASP.NET MVC
Developers http://bit.ly/cVKYp5
Language Oriented Programming: The Next Programming Paradigm: http://bit.ly/bVqYoG
CodePlex: http://bit.ly/i0VnHK Book
Domain-Specific Development with Visual Studio DSL Tools ISBN: 0321398203
Resources
Visual Studio Plug-ins Plug-in by Tangible Engineering
http://bit.ly/dfFhEi T4 Editor by Clarius
http://bit.ly/cHfGRn
Resources
Q&A
T4 / Code Generation