madhavrao… » 2008 » March

Host in T4 is responsible for anything that relates to external environment, including the following:

  1. Providing a file or an assembly if the engine or a directive processor requests one.
  2. The host can search directories and the global assembly cache to locate files and assemblies. (This is implemented in “ResolveAssemblyReference” method of host class)
  3. The host can locate custom directive processor code for the engine. (This is implemented in “ResolveDirectiveProcessor” method of host class)
  4. The host can open and read files and return their contents as strings. (This is implemented in “LoadIncludeText” method of host class. If it returns true then included text in “content” parameter and location of the file in “location” paramter)
  5. Providing lists of standard assemblies and namespaces to the engine. (This is implemented in host class as “StandardAssemblyReferences” and “StandardImports” properties)
  6. The engine uses these assemblies and namespaces when it creates the generated transformation class as part of the text template transformation process.
  7. Providing the application domain that the engine uses to compile and execute the generated transformation class. (This is implemented in “ProvideTemplatingAppDomain” method of host class)
  8. Writing the generated text output file. (Host provides file-encoding and file content to the calling code, actual writing is done my class which holds engine, and host instance)
  9. Setting the default extension for the generated text output files. (Default extension can be changed in host object at runtime by calling “SetFileExtension” method. In case extension is specified in template file using “output” directive it will override everything else.)
  10. Receiving the text template transformation errors from the engine and deciding what to do with them. For example, the host can display the errors in the user interface or write them to a file. (This is implemented in “LogErrors” method of host class. Engine calls this method to pass compile errors to host)
  11. Helping a directive processor resolve a required parameter value if a user has called a directive without providing a value. (This is implemented in “ResolveParameterValue” method of host class)
  12. The directive processor can specify the name of the directive and the parameter and ask the host to provide a default value if it has one.

If you want to use the text template transformation functionality in a custom application, you can write a custom text template host. To create a custom host, you create a class that inherits from ITextTemplatingEngineHost

The text template transformation process has three components: the host, the engine and the directive processor.

Engine: The engine component controls the process and interacts with the host and zero or more directive processors to complete the process.

Host: The host is responsible for all input and output, locating files and anything else related to external environment. Visual Studio is example of Host, one can create custom host by implementing interface ITextTemplatingEngineHost from Microsoft.VisualStudio.TextTemplate assembly. Details of Creating Custom Text Template Host are covered in MSDN

Directive Processors: Directive processors are classes that handle directives in text templates. One can typically use directives to provide data from an input source (for example, from a model) to a text template. When you use Domain-Specific Language Tools, a directive processor is generated for each domain-specific language, so that text templates can access the models in that language as input sources. One can also write custom directive processors. Details of Creating Custom Text Template Directive Processors is covered in MSDN.

Text Template Transformation process is a two step process, in first step temporary class (generated transformation class) is created based on text template and in second step engine complies and executes the generate transformation class to generate text output.

Text Template Transformation Toolkit (T4) is used primarily for tools like Guidance Automation Toolkit (GAT) and Domain Specific Language (DSL).

T4 allows processing of text templates. Text templates is a file that contains mixture of text and control logic.When T4 transform a text template, the control logic combines the text blocks with the data in a model to produce an output file. One can use text templates to create text artifacts such as code files and HTML reports.

The following are examples of tasks that can be performed by using text templates:

  • After creating models, one can iterate over their in-memory-representation (instead of their raw XML representation) to generate arbitrary text.
  • One can generate custom code by using either Visual C# or Visual Basic to navigate through a model.
  • One can generate custom reports in XML or HTML.

T4 can be used with both Visual Studio 2005 and Visual Studio 2008.

Clarius Consulting has developed a free download called the T4 Editor that integrates the T4 design-time experience directly into Visual Studio 2005 and 2008, in the process offering support for colorization of directives, code and text blocks, IntelliSense and design-time previews of templates. The download supports Visual Basic and C# in control logic.

T4 can also be used outside Visual Studio in custom tool, also command line transformation tool “TextTransform.exe” is readily available to be used outside Visual Studio. There is good information available on MSDN on how to use “Command Line Tools for Text Template”