5. 50,000 Foot View
• Domain Specific
Development approach
• (If applicable)
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
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. 10,000 foot view
• T4
• Engine to create text-based DSLs
• Visual Studio
• 2005 -GAT toolkits
• 2008 - Downloadable DSL toolkit
• 2010 - Integrated part
10. 5000 foot view
• Generates code from described models
• Models could be
• Metadata
• Configuration
• Database
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
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. What is T4?
• Text Template
Transformation Toolkit
• A templated text
generation engine
19. T4 Usage In Visual Studio
• ASP
.NET MVC
• Visual Studio Item templates
• Visual Studio Project templates
20. T4 Structure
• An overview of the T4 anatomy
• Processing a Template
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
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” #>
40. Visual Studio – support tools
• Plug-in by Tangible Engineering *
• http://bit.ly/dfFhEi
• T4 Editor by Clarius
• http://bit.ly/cHfGRn
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. Resources
• All resource links: http://www.joubin.ca
• T4 Templates: A Quick-Start Guide for ASP
.NET
MVC Developers
• http://bit.ly/cVKYp5
• Code gen + text template: http://bit.ly/VYsFf
• Glossary of terms: http://bit.ly/9gZosy
• Oleg’s blog: http://bit.ly/cq13qq
43. Resources
• Language Oriented Programming: The Next
Programming Paradigm:
• http://bit.ly/bVqYoG
• Book: Domain-Specific Development with
Visual Studio DSL Tools
• ISBN: 0321398203
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
Metadata
Configuration
Database
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
Directives
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 #>
Directives
template
assembly
import
output
include
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
Inherits
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
Host.TemplateFile
Host.StandardAssemblyReferenes
“Add reference” in VS.NET
One parameter
name
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
namespace
File to be included verbatim in template
One parameter
File
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
%TEMP%
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
Fields
Constants
Properties
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.