Freigeben über


Accessing Models from Text Templates

By using text templates, you can create reports, code, or other artifacts from models. For more information, see Generating Artifacts Using Text Templates. Before you can access a model from the statements and expressions in a text template, you must first call a generated directive processor.

Generated Directive Processors

When you use Domain-Specific Language Tools to create a domain model, the tools generate a directive processor for your domain model.

Hinweis

You can find the code for the generated directive processor in the following location: <YourSolutionName>\Designer\Text Templates\DirectiveProcessor.dslddt\DirectiveProcessor.cs

The name of the generated directive processor depends on the name of your language, which you specified in the Domain-Specific Language Designer Wizard.

Hinweis

You can find the name of your language in the name attribute of the <designerDefinition> tag in the file Designer\Designer.dsldd in your solution.

The name of the directive matches the name of the root domain class in your domain model. The default name of the root domain class depends on the solution template on which you based your language. The following table shows the default directive names:

Solution Template

Directive Name

Directive Processor Name

Activity Diagrams

ActivityGraph

<YourLanguageName>DirectiveProcessor

Class Diagrams

ModelRoot

<YourLanguageName>DirectiveProcessor

Minimal Language

ExampleModel

<YourLanguageName>DirectiveProcessor

Use Case Diagrams

UseCaseModel

<YourLanguageName>DirectiveProcessor

Calling Generated Directive Processors

Before you can access a model from your text template code, you must call the generated directive processor. Calling the generated directive processor makes the classes in your model available as properties to the text template code. The following table shows example calls to the generated directive processor.

Solution Template

Directive Processor Call

Activity Diagrams

<#@ ActivityGraph processor="<YourLanguageName>DirectiveProcessor" requires="fileName='Sample.aaa'" provides="ActivityGraph=ActivityGraph" #>

Class Diagrams

<#@ ModelRoot processor="<YourLanguageName>DirectiveProcessor" requires="fileName='Sample.ccc'" provides="ModelRoot=ModelRoot" #>

Minimal Language

<#@ ExampleModel processor="<YourLanguageName>DirectiveProcessor" requires="fileName='Sample.mmm'" provides="ExampleModel=ExampleModel" #>

Use Case Diagrams

<#@ UseCaseModel processor="<YourLanguageName>DirectiveProcessor" requires="fileName='Sample.uuu'" provides="UseCaseModel=UseCaseModel" #>

Hinweis

Your file names will differ from the examples in this table. Your file extensions will match the file extension that you specified for your language in the Domain-Specific Language Designer Wizard.

The Requires and Provides Parameters

When you call a generated directive processor, you specify values for the requires parameter and the provides parameter.

In the requires parameter, you specify the name of the file that contains the model that you want to access in your text template. If you want to access more than one model from the same text template, you must call the generated directive processor again and specify the file name of the second model. For more information, see Accessing Multiple Models from a Text Template later in this topic.

In the provides parameter, you specify the name that you want to use to access your model. By default, you specify the name of the generated directive as the value for the provides parameter. For example:

<#@ ExampleModel processor="<YourLanguageName>DirectiveProcessor" requires="fileName='Sample.mmm'" provides="ExampleModel=ExampleModel" #>

If you do not want to use the default name, you can specify your own value for the provides parameter. For example:

<#@ ExampleModel processor="<YourLanguageName>DirectiveProcessor" requires="fileName='Sample.mmm'" provides="ExampleModel=MyLibraryModel" #>

You cannot use the default provides parameter value more than once. If you want to access more than one model from the same text template, you must specify a different value for all the provides parameters after the first directive call. For more information, see Accessing Multiple Models from a Text Template later in this topic.

Accessing a Model from a Text Template

After you have called the generated directive processor, you can access the model in your text template code. You refer to your model using the name that you specified in the provides parameter of your directive call. The following table lists examples of text template code that accesses models. In these examples, the default name was specified in the provides parameter, and the corresponding reference to the model is highlighted.

Solution Template

Sample Text Template Code

Activity Diagrams

<#
foreach (ActivityElement task in this.ActivityGraph.Elements )
...
#>
<#
For Each task As ActivityElement In Me.ActivityGraph.Elements
...
#>

Class Diagrams

<#
foreach (ModelType type in this.ModelRoot.Types)
#>
<#
For Each type As ModelType In Me.ModelRoot.Types
...
#>

Minimal Language

<#
foreach (ExampleClass box in this.ExampleModel.Elements)
...
#>
<#
For Each box As ExampleClass In Me.ExampleModel.Elements
...
#>

Use Case Diagrams

<#
foreach (UseCaseElement element in this.UseCaseModel.UseCaseElements)
...
#>
<#
For Each element As UseCaseElement In Me.UseCaseModel.UseCaseElements
...
#>

Accessing Multiple Models from a Text Template

If you want to access more than one model from the same text template, you must call the generated directive processor once for each model that you want to access. You must specify the file name of each model in the requires parameters. You must specify the names that you want to use to access your models in the provides parameters. You must specify different values for the provides parameters in each of the directive calls. You cannot use the default values for each of the directive calls.

For example, assume you have three model files called Library.xyz, School.xyz, and Work.xyz. To access them from the same text template, you must write three directive calls like the following:

<#@ ExampleModel processor="<YourLanguageName>DirectiveProcessor" requires="fileName='Library.xyz'" provides="ExampleModel=LibraryModel" #>
<#@ ExampleModel processor="<YourLanguageName>DirectiveProcessor" requires="fileName='School.xyz'" provides="ExampleModel=SchoolModel" #>
<#@ ExampleModel processor="<YourLanguageName>DirectiveProcessor" requires="fileName='Work.xyz'" provides="ExampleModel=WorkModel" #>

Hinweis

This example code is for a language that is based on the Minimal Language solution template.

Hinweis

This example code specifies different values for the provides parameter in all three directive calls. None of the directive calls uses the default value ExampleModel for the value of the provides parameter.

To access the models in your text template, you can now write code like the following:

<#
foreach (ExampleClass box in this.LibraryModel.Elements)
...
foreach (ExampleClass box in this.SchoolModel.Elements)
...
foreach (ExampleClass box in this.WorkModel.Elements)
...
#>
<#
For Each box As ExampleClass In Me.LibraryModel.Elements
...
For Each box As ExampleClass In Me.SchoolModel.Elements
...
For Each box As ExampleClass In Me.WorkModel.Elements
...
#>

Debugging

To debug text templates, you must set the debug parameter of the template directive. For more information, see How to: Debug Text Templates.

Security

For more information, see Security of Text Templates.

See Also

Concepts

How to: Call a Generated Directive

How to: Test Domain-Specific Language Solutions

Walkthrough: Creating and Running Text Templates

Architecture of Text Templates