Generating Code from a Domain-Specific Language


The new home for Visual Studio documentation is Visual Studio 2017 Documentation on

The latest version of this topic can be found at Generating Code from a Domain-Specific Language.

Microsoft Domain-Specific Language Tools provides a powerful way to generate code, documents, configuration files and other artifacts from data represented in models. Using Domain-Specific Language Tools, you can create a set of classes that represent your data, and you can write your text templates in classes whose names and properties reflect that data.

For example, Fabrikam has an XML file of customer names and e-mail addresses. Their developers create a model in which Customer is a class, with properties name and e-mail. They write several text templates to process the data, including this fragment which produces a table of all the customers as part of an HTML page:

<# foreach (Customer c in ContactList) {  #>  
  <tr><td> <#= c.FullName #> </td>   
      <td> <#= c.EmailAddress #> </td> </tr>  
<# } #>  </table>  

When the customer database is processed, the XML file is read into the model store. A directive processor, created by using Domain-Specific Language Tools, makes the Customer class available to the code in the text template. Many text templates can be run against the same store.

Text templates are essential to Domain-Specific Language Tools. They are used to generate the source code for the elements of the domain model as well as for the VSPackage and the controls that are used to integrate the tools with Visual Studio.

This section discusses some of the ways to create, modify, and debug text templates used in Domain-Specific Language Tools.

Accessing Models from Text Templates

Provides basic information about referring to domain-specific language in text templates.

Walkthrough: Debugging a Text Template that Accesses a Model

Describes how to do troubleshooting and debugging on a text template that refers to a domain-specific language.

Walkthrough: Connecting a Host to a Generated Directive Processor

Describes how to connect a custom host to a generated directive processor.

The DslTextTransform Command

Describes the command file that executes the TextTransform executable on the command line for text templates that reference domain-specific languages.

Writing a T4 Text Template

Provides the syntax of text template directives and control blocks.

Design-Time Code Generation by using T4 Text Templates

Explains the text template transformation process.

Code Generation in a Build Process

Read this topic if you are generating files from a DSL on a build server.