T4 / Code Generation

40
T4 / Code Generation Presented by Joubin Najmaie (@codekindler) February 23, 2011 Kitchener .NET UG: @cttdnug

description

This presentation was made to the Kitchener .NET UG on Feb. 23, 2011

Transcript of T4 / Code Generation

Page 1: T4 / Code Generation

T4 / Code Generation

Presented by Joubin Najmaie (@codekindler)

February 23, 2011

Kitchener .NET UG: @cttdnug

Page 2: T4 / Code Generation

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

Page 3: T4 / Code Generation

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?

Page 4: T4 / Code Generation

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

Page 5: T4 / Code Generation

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

Page 6: T4 / Code Generation

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

Page 7: T4 / Code Generation

T4 Syntax

Page 8: T4 / Code Generation

Anatomy of T4 templates

T4 Syntax

Page 9: T4 / Code Generation

Text Templating ‘workflow’

T4 Syntax

Page 10: T4 / Code Generation

Processing Directives

T4 Syntax

Similar to ASPX or ASCX <#@ and end with #>

Directives template assembly import output include

Page 11: T4 / Code Generation

template directive

T4 Syntax

<#@ template debug="true" language="C#" #>

Parametersdebug language inheritshostspecific

Page 12: T4 / Code Generation

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

Page 13: T4 / Code Generation

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” #>

Page 14: T4 / Code Generation

import directive

T4 Syntax

<#@ import #> Parameter

namespaceE.g. <#@ import

namespace=“System.Collections.Generic” #>

Page 15: T4 / Code Generation

include directive

T4 Syntax

<#@ include #> Parameter

file e.g. <#@ include file=“includedfile.tt” #>

Page 16: T4 / Code Generation

Demodirectives

Page 17: T4 / Code Generation

On to T4 blocks

T4 Syntax

Page 18: T4 / Code Generation

T4 Blocks

T4 Syntax

Text blocks Code blocks

Statement Expression Class feature

Page 19: T4 / Code Generation

Text Blocks

T4 Syntax

Non-programmatic plain text

Page 20: T4 / Code Generation

Statement Blocks

T4 Syntax

Delineated using <# #>

Page 21: T4 / Code Generation

Expression Blocks

T4 Syntax

Delineated using <#= #>

Page 22: T4 / Code Generation

Class Feature Blocks

T4 Syntax

Delineated using <#+ #>

Add any class level constructHelper functionsFieldsConstantsProperties

Page 23: T4 / Code Generation

DemoT4 Blocks

Page 24: T4 / Code Generation

Debugging

Debugging T4 Templates

Insert System.Diagnostics.Debugger.Launch(); anywhere in the code blocks

Page 25: T4 / Code Generation

DemoDebugging T4 Templates

Page 26: T4 / Code Generation

Break ?

Page 27: T4 / Code Generation

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

Page 28: T4 / Code Generation

DemoCustom T4 Host

Page 29: T4 / Code Generation

DemoCompile Generated Code

Page 30: T4 / Code Generation

VS T4 Item Templates

T4 Item Templates

Text Template TextTemplatingFileGenerator

Preprocessed Text Template TextTemplatingFilePreprocessor Redistributing T4 engine with your app

Page 31: T4 / Code Generation

DemoT4 Item Templates

Page 32: T4 / Code Generation

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

Page 33: T4 / Code Generation

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

Page 34: T4 / Code Generation

VS Template files

VS 2010 Item/Project Templates

Single File Template Multi-File Template

Page 35: T4 / Code Generation

DemoVS 2010 Item/Project Templates

Page 36: T4 / Code Generation

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

Page 37: T4 / Code Generation

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

Page 38: T4 / Code Generation

Visual Studio Plug-ins Plug-in by Tangible Engineering

http://bit.ly/dfFhEi T4 Editor by Clarius

http://bit.ly/cHfGRn

Resources

Page 39: T4 / Code Generation

Resources will be posted

Blog: http://www.codekindler.com

Email: [email protected]

Resources

Page 40: T4 / Code Generation

Q&A

T4 / Code Generation