Your SlideShare is downloading. ×
T4 / Code Generation
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

T4 / Code Generation

3,071
views

Published on

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

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

Published in: Technology, Business

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
3,071
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
36
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • Characteristics of the transformed outputDebug --- true/falseTrue – you can take a look files in system %TEMP% folderLanguage: Values: C#, VB, C#3.5InheritsBase class: Microsoft.VisualStudio.TextTemplating.TextTransformationTells which base class to useHostspecific --- true/falseMostly used to output template file name to the transformed fileHost.TemplateFileHost.StandardAssemblyReferenes
  • “Add reference” in VSOne parameternamecould be an assembly in the GAC<#@ assembly name=“System.Data” #>or absolute path to the assembly<#@ assembly name=“c:\\projects\\proj1\\bin\\debug\\proj1.dll” #>
  • Equivalent to: C# using VB.NET importOne parameternamespace
  • File to be included verbatim in templateOne parameterFileCan contain any of the valid T4 constructsRelative paths -- to the main template
  • Just type directly into the .tt file Copied directly to TransformText method%TEMP%Show generated file
  • Control the flow of processing in TransformText methodExample usage:To conditionally output Text blocksIterate over data and output things conditionally
  • Use to add strings to the output fileAdded, in sequenceTo the “TransformText” method with calls to the ToString() methodUsed in cases to change some aspect of the generated output text file to useCalculated valuesExternal data e.g. database column valuessome other calculated value in a Statement block somewhere else in the .TT file. C# – could be multiple linesVB.NET – single lines only
  • Transcript

    • 1. T4 / Code Generation
      Presented by Joubin Najmaie (@codekindler)
      February 23, 2011
      Kitchener .NET UG: @cttdnug
    • 2. 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
    • 3. 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: joubin.najmaie@gmail.com
      Who am I?
    • 4. Motivations Behind Using T4
      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
    • 5. 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
    • 6. 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 managedwith 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
    • 7. T4 Syntax
    • 8. T4 Syntax
      Anatomy of T4 templates
    • 9. T4 Syntax
      Text Templating ‘workflow’
    • 10. T4 Syntax
      Processing Directives
      Similar to ASPX or ASCX
      <#@ and end with #>
      Directives
      template
      assembly
      import
      output
      include
    • 11. T4 Syntax
      template directive
      <#@ template debug="true" language="C#" #>
      Parameters
      debug
      language
      inherits
      hostspecific
    • 12. T4 Syntax
      template directive parameters
      debug parameter
      System %TEMP% folder
      language parameter
      Values: C#, VB, C#3.5
      inherits parameter
      Tells which base class to use
      hostspecific parameter
      A reference to the object that the hosts the engine.
      e.g. Host.TemplateFile
      Host.StandardAssemblyReferences
    • 13. T4 Syntax
      assembly directive
      <#@ assembly #>
      Parameter
      name
      could be an assembly in the GAC
      <#@ assembly name=“System.Data” #>
      absolute path to the assembly
      <#@ assembly name=“c:projectsproj1bindebugproj1.dll” #>
    • 14. T4 Syntax
      import directive
      <#@ import #>
      Parameter
      namespace
      E.g. <#@ import namespace=“System.Collections.Generic” #>
    • 15. T4 Syntax
      include directive
      <#@ include #>
      Parameter
      file
      e.g. <#@ include file=“includedfile.tt” #>
    • 16. Demo
      directives
    • 17. T4 Syntax
      On to T4 blocks
    • 18. T4 Syntax
      T4 Blocks
      Text blocks
      Code blocks
      Statement
      Expression
      Class feature
    • 19. T4 Syntax
      Text Blocks
      Non-programmatic plain text
    • 20. T4 Syntax
      Statement Blocks
      Delineated using
      <# #>
    • 21. T4 Syntax
      Expression Blocks
      Delineated using
      <#= #>
    • 22. T4 Syntax
      Class Feature Blocks
      Delineated using
      <#+ #>
      Add any class level construct
      Helper functions
      Fields
      Constants
      Properties
    • 23. Demo
      T4 Blocks
    • 24. Debugging T4 Templates
      Debugging
      Insert System.Diagnostics.Debugger.Launch(); anywhere in the code blocks
    • 25. Demo
      Debugging T4 Templates
    • 26. Break ?
    • 27. Custom T4 Host using GAX
      Custom T4 Host
      Guidance Automation Extensions
      http://bit.ly/fW99Zh
      Add references in VS
      Microsoft.Practices.RecipeFramework.VisualStudio.Library
      Microsoft.VisualStudio.TextTemplating
    • 28. Demo
      Custom T4 Host
    • 29. Demo
      Compile Generated Code
    • 30. T4 Item Templates
      VS T4 Item Templates
      Text Template
      TextTemplatingFileGenerator
      Preprocessed Text Template
      TextTemplatingFilePreprocessor
      Redistributing T4 engine with your app
    • 31. Demo
      T4 Item Templates
    • 32. VS 2010 Item/Project Templates
      VS Item Templates
      Single file versus multi-file templates
      As .vsi/.zip
      VS 2010
      /{vs.net folder}/common7/ide/itemtemplates
      /{user folder}Visual Studio 2010TemplatesItemTemplates
      Template parameters
      When the template wizard runs they are replaced
    • 33. VS 2010 Item/Project Templates
      Parameters to a VS template file
      Complete list of all parameters: http://bit.ly/hJayFX
    • 34. VS 2010 Item/Project Templates
      VS Template files
      Single File Template
      Multi-File Template
    • 35. Demo
      VS 2010 Item/Project Templates
    • 36. 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
    • 37. Resources
      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
    • 38. Visual Studio Plug-ins
      Plug-in by Tangible Engineering
      http://bit.ly/dfFhEi
      T4 Editor by Clarius
      http://bit.ly/cHfGRn
      Resources
    • 39. Resources will be posted
      Blog: http://www.codekindler.com
      Email: joubin.najmaie@gmail.com
      Resources
    • 40. T4 / Code Generation
      Q&A