Domain Specific Development using T4


Published on

Toronto Architecture User Group presentation

Published in: Technology, Business
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Main concepts of DSD
    Demo project features
  • 1) A task/project/problem in hand
    2) Pick favorite general purpose language – C#, VB.NET
    3) Deliver project

    Each occurrence of problem has many aspects that are the same everytime (fixed part)
    And other aspects that are differs each time – variable part
    We’re going to automatically generate code for the variable part of the solution.

    Fixed part – We’ll treat as the framework
    Variable aspect – fits into the framework itself

    On a current project:
    Fixed aspect: Web Asset and configuration management framework
    Variable aspect: The content models (or aka content types in SharePoint)

    The content types: defined as metadata in a database, configuration file etc.
    T4 templates run to generate special classes (and logic) for the specific content types.
    The generated code could then be embedded in VS.NET projects (code or assembly) or called dynamically on the server to retrieve data.

    Other examples: ORM software

    Language-oriented programming

    Basis – observation
    Instead of solving problem one-at-a-time
    Many problems can more easily be resolved by designing a special-purpose language

    Code generation – through DSL toolkit with VS.NET

    Use T4 to generate that specialized programming language

  • 1) A task/thought/problem in hand
    2) Create/Pick a special-purpose language
    3) Use a general purpose lang to write framework
    4) Deliver project
  • -- Benefits

    Ability to work in terms of the problem space

    Working in terms of the model of the problem space makes it more accessible to people not familiar with the problem space

    You can have the domain-experts verify the correctness of your model independently

    The model itself should be more testable and used to simulate scenarios

    Once the business knowledge is captured, the DSL could be updated to use other technologies, for example, by modifying the generator
  • Framework – use general-purpose languages

    The variable pieces –generate domain-specific languages

    Plug the generated domain-specific languages into the framework
  • DSL = “custom language that targets a small problem domain, which it describes and validates in terms native to the domain.”

    Could be seen as a set of tools to build tools

    Focus: text-based only!
  • T4 - Engine to create text-based DSLs

    Visual Studio.NET
    2005 - GAT toolkits
    2008 - Downloadable DSL toolkit
    2010 - Integrated part
  • Generates code from described models

    Models could be

    What does it stand for?

    What are the Inputs?

    What are the outputs?

    What does the syntax look like?

    What are the supported languages?
  • T3 == Text-to-Text Transformation

    T4 == Text Template Transformation Toolkit

    A tool in the VS.NET DSL toolkit

    A text-based code generation engine

    Templates = syntax similar to ASPX or ASCX

    Support: C# and VB.NET
  • VS.NET extensibility
  • .tt

    Pre 2010 – text file – turn it into .tt

    2010 – two kinds of item templates

    Default transformation tools – properties of a tt file

    Publishing: Build Action to Content
  • ASP.NET-like syntax

    Processor directives: compilation options

    Blocks: Mixture of text, control logic, blocks of executable code
  • Base class: Microsoft.VisualStudio.TextTemplating.TextTransformation


    Assemblies are loaded

    Include files are imported

    Text and expression blocks: Copied to the TransformText method

    Class-feature blocks: Copied at the class level

    T4 engine – calls the “TransformText” method – entry point

    %TEMP% folder

  • This demo will be the basis for all Syntax reviewed here.
  • Provide instructions to the T4 engine

    Just like ASPX pages:
    <#@ and end with #>

  • Characteristics of the transformed output

    Debug --- true/false
    True – you can take a look files in system %TEMP% folder

    Language: Values: C#, VB, C#3.5

    Base class: Microsoft.VisualStudio.TextTemplating.TextTransformation
    Tells which base class to use

    Hostspecific --- true/false
    Mostly used to output template file name to the transformed file
  • “Add reference” in VS.NET

    One parameter

    could 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 import

    One parameter
  • File to be included verbatim in template

    One parameter

    Can contain any of the valid T4 constructs

    Relative paths -- to the main template

  • Had an overview of the processing directives

    Let’s talk about T4 block types
  • Just type directly into the .tt file

    Copied directly to TransformText method

    Show generated file
  • Control the flow of processing

    in TransformText method

    Example usage:

    To conditionally output Text blocks
    Iterate over data and output things conditionally
  • Use to add strings to the output file

    Added, in sequence
    To the “TransformText” method with calls to the ToString() method

    Used in cases to change some aspect of the generated output text file to use
    Calculated values
    External data e.g. database column values
    some other calculated value in a Statement block somewhere else in the .TT file.
    C# – could be multiple lines
    VB.NET – single lines only
  • Any class-level construct
    Helper functions
    any construct that is valid at the class level

    T4 allows the usage of text and expression blocks intermingled with class feature blocks

    The helper method would be called from within a Statement block.
  • Text template - %TEMP%

    Preprocessed: uses TextTemplatingFilePreprocessor transformation tool

    Debugging: System.Diagnostics.Debugger.Launch();
  • Domain Specific Development using T4

    1. 1. Use T4 to apply Domain Specific Development Joubin Najmaie (@joubin) Senior Solution Architect
    2. 2. Agenda • Domain Specific Development – quick concepts • Quick story behind it all • Project overview • Break • Review and Recap • T4 overview
    3. 3. All Resources and Links
    4. 4. 60,000 Foot View • Our typical project iterations
    5. 5. 50,000 Foot View • Domain Specific Development approach • (If applicable)
    6. 6. 40,000 Foot View • Work in terms of the problem space • Makes it more accessible • people not familiarwith the problem space • Models - more verifiable and updatable • Business knowledge is captured once • DSL could be updated
    7. 7. 30,000 Foot View • Framework • DSL – variable
    8. 8. 20,000 foot view • Domain-specific languages (DSLs) • Text-based • Code, XML, sql, wsdl, documentation etc. • Graphical • Flowcharts, ERDs, state diagrams, Venn diagrams
    9. 9. 10,000 foot view • T4 • Engine to create text-based DSLs • Visual Studio • 2005 -GAT toolkits • 2008 - Downloadable DSL toolkit • 2010 - Integrated part
    10. 10. 5000 foot view • Generates code from described models • Models could be • Metadata • Configuration • Database
    11. 11. One Story Behind our Project • Our “suit” calls a meeting • Empower the business users • Give control over their own content • Decentralize responsibilities • Take “work” out • By giving control over their content
    12. 12. Run Back to Your Desk
    13. 13. Content Analysis and Req’t Gathering
    14. 14. Code and Code more
    15. 15. Moment of Realization • You’re doing the same thing every time you want to be able to content manage a portion of the site
    16. 16. Project Walkthrough PageFragments
    17. 17. Review & Recap • Create a Special Purpose Language • Capture of Domain knowledge • Accessible to more developers • Ability to build upon models • Model is verifiable • Domain Specific API for easy manipulation • Flexibility: DSL could be in any language
    18. 18. What is T4? • Text Template Transformation Toolkit • A templated text generation engine
    19. 19. T4 Usage In Visual Studio • ASP.NET MVC • Visual Studio Item templates • Visual Studio Project templates
    20. 20. T4 Structure • An overview of the T4 anatomy • Processing a Template
    21. 21. Text-template • Extension: .tt • Item templates: • Text Template • Preprocessed text template • Default transformation tools in Visual Studio • TextTemplatingFileGenerator • TextTemplatingFilePreprocessor • In order to deploy/publish • Build Action: Content
    22. 22. Anatomy of a Text-Template
    23. 23. How a Text-Template is Processed
    24. 24. Generate Class Based On an XSD
    25. 25. Let’s Get Into The T4 Syntax • An overview of the T4 syntax • Processing directives • T4 various blocks
    26. 26. Processing Directives Similar to ASPX or ASCX directive syntax <#@ and end with #> Directives template assembly import output include
    27. 27. Processing Directive - template <#@ template debug="true" language="C#" #> Parameters debug language inherits hostspecific
    28. 28. Processing Directive - assembly <#@ assembly #> Parameter name could be an assembly in the GAC • <#@ assembly name=“System.Data” #> or absolute path to the assembly • <#@ assembly name=“c:projectsproj1bindebugproj1.dll” #>
    29. 29. Processing Directive - import <#@ import #> Parameter namespace <#@ import namespace=“System.Collections.Generic” #>
    30. 30. Processing Directive - include <#@ include #> Parameter file <#@ include file=“” #>
    31. 31. Processing Directives
    32. 32. T4 Syntax • Processing directives • T4 various blocks
    33. 33. Text Blocks Non-programmatic text
    34. 34. Statement Blocks Delineated using <# #>
    35. 35. Expression Blocks Delineated using <#= #>
    36. 36. Class Feature Blocks Delineated using: <#+ #> Used to add any class level construct Helper functions Fields Constants Properties
    37. 37. Code Blocks
    38. 38. Debugging a Text Template
    39. 39. Compile and Generate Assembly
    40. 40. Visual Studio – support tools • Plug-in by Tangible Engineering * • • T4 Editor by Clarius •
    41. 41. Call to action • 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
    42. 42. Resources • All resource links: • T4 Templates: A Quick-Start Guide for ASP.NET MVC Developers • • Code gen + text template: • Glossary of terms: • Oleg’s blog:
    43. 43. Resources • Language Oriented Programming: The Next Programming Paradigm: • • Book: Domain-Specific Development with Visual Studio DSL Tools • ISBN: 0321398203
    44. 44. Evaluations and Feedback • Please fill out your evaluations!
    45. 45. Thank you! And thanks to our sponsors
    46. 46. Send T4 questions to: • Joubin Najmaie • Twitter: @joubin • Email: • Blog: