Now that we have built a sample business entity diagram, you may be wondering what you can do with it. In this task we’ll demonstrate how to use the text generation functionality included with the DSL toolkit to generate artifacts from our model. Text generation works via template files, which contain both templatized text to be generated, and logic which controls how that generation happens. Let’s look at an example which takes a .biz file as input, and generates an HTML report over the domain defined in that file.
Right-click on the BizEntityDebugging project in the Solution Explorer, and choose Add->Existing Item… from the context menu.

Set the Files of Type: filter to All Files.
Type c:\dsl\BizEntity.ReportTemplate into the File name: box, and press Add.
Double-click on the newly added BizEntity.ReportTemplate file in the Solution Explorer to open it in the text editor (you may want to close the Toolbox window at this point to give the text editor more room). We will examine the details of this file to see how code generation works.
The first three lines, shown below, set up some parameters for the code generator, we won’t concern ourselves with the details of those here:
<#@ template inherits="Microsoft. VisualStudio.TextTemplating.
VSHost.ModelingText Transformation"#>
<#@ output extension=".htm" #>
<#@ er processor="BizEntity DirectiveProcessor"
requires="fileName='Empty.
biz'" #>
The next five lines are standard HTML, these will be generated to the output file as static header text:
<html>
<body>
<h1>A report on the contents of Empty.biz</h1>
<p>For this report to work…</p>
<h2>Entities:</h2>
Now things get interesting. Notice the <# and #> tags which delimit sections of the file. Logic which controls text generation in placed within these tags (if you are familiar with ASP pages, this is a similar idea). In this case, the logic is written in C#, but it may be written in Visual Basic .NET as well. To make clearer what’s going on, we’ve grayed out the text to be generated below so you can just focus on the control flow; if you are familiar with C#, you will notice that what’s left are two nested for each loops, the outer one over a collectio n called “Entities” and the inner one over a collection called “Attributes”. As you may have guessed, these collections contain elements instances from the domain model.
<#
foreach ( Entity e in this.ER.Entities )
{
#>
<h3><#=e.Name#></h3>
<p>Kind: <#=e.Kind#></p> <#=e.Kind#></p>
<#
foreach ( Attribute a in e.Attributes )
{
#>
<p>Attribute: <#=a.Name#>
<#
}
#>
<#
}
#>
Now let’s look at the same section of the file, but focus on the generated text. As in the static header, these lines are HTML, except for the <#= #> tags, which indicate parameters that will be filled in by values from the domain model during generation. Note the use of the properties “Name” and “Kind” to indicate which values from the domain model will be used. Also note the use of the foreach loop iteration variables “e” and “a” to indicate which Entity and Attribute instances are used in the parameterization (these have been bolded for emphasis).
<#
foreach ( Entity e in this.ER.Entities )
{
#>
<h3><#=e.Name#></h3>
<p>Kind: <#=e.Kind#></p>
<#
foreach ( Attribute a in e.Attributes )
{
#>
<p>Attribute: <#=a.Name#>
<#
}
#>
<#
}
#>
Next, let’s run the template. Click on the BizEntity.ReportTemplate file in the Solution Explorer.
In the Properties window, enter the value TextTemplatingFileGenerator for the Custom Tool property, and press Enter.

Notice that a “+” appears next to the BizEntity.ReportTemplate file in the Solution Explorer. Click on this to reveal the generated file, BizEntity.htm. Right-click on this file and choose View in Browser from the context menu to see it rendered in Internet Explorer.

Notice that the Entities and Attributes defined in the business entity diagram appear in this report. Due to the interaction logic in the template, if we were to add additional Entities or Attributes, and re-run text generation, those additional items would appear in the report.
Close the BizEntity.htm file