Home > Templates > The Template Producer

The Template Producer

We introduced the text template engine in the previous post with a small example illustrating how to generate a simple text file at run time. Now in this post, I’d like to introduce the Template Producer which is an out-of-the-box producer and uses the template engine to allow you to generate text files at production time.

Say you have the following model (XML):

        <Customer typeName="Customer">        
        <Lines typeName="InvoiceLineCollection">        

       <Id />

       <Id />

Let’s say we’d like to generate a DGML graph from our model. The equivalent in DGML would be something like:

<?xml version="1.0" encoding="utf-8"?>
<DirectedGraph xmlns="http://schemas.microsoft.com/vs/2009/dgml"&gt;
        <Node Id="Invoice" Description="" Label="Invoice" />
        <Node Id="InvoiceLine" Description="" Label="InvoiceLine" />        
        <Node Id="Customer" Description="" Label="Customer" />        
        <Link Source="Invoice" Target="Customer" Label="Customer"/>
        <Link Source="Invoice" Target="InvoiceLine" Label="Lines"/>

Well let’s create ourselves a template to generate this DGML graph:

<?xml version="1.0" encoding="utf-8"?>
<DirectedGraph xmlns="http://schemas.microsoft.com/vs/2009/dgml"&gt;
        [%foreach(Entity entity in Producer.Project.Entities){%]
        <Node Id="[%=entity.ClrFullTypeName%]" Description="" Label="[%=entity.Name%]" />
        [%foreach(Relation relation in Producer.Project.Relations){ if (relation.RelationOrigin != RelationOrigin.Declared) continue; %]
        <Link Source="[%=relation.Entity.ClrFullTypeName%]" Target="[%=relation.RelatedEntity.ClrFullTypeName%]" Label="[%=relation.Property.Name%]" />
        <Style TargetType="Node">
              <Setter Property="MinWidth" Value="10" />
              <Setter Property="NodeRadius" Value="10" />
              <Setter Property="Background" Value="Green" />

Note: As mentioned in our previous post you can see that we’re using [%= … %] to print expression values, and [% … %] to embed C# statements.

Basically, what the template does is that it iterates through all entities of the project to write the Nodes section, and then iterates throughout all relations of the project to write the Links one.

In your model you then need to define a Template Producer instance:

<cf:producer typeName="CodeFluent.Producers.CodeDom.TemplateProducer, CodeFluent.Producers.CodeDom">
    <cf:configuration sourceDirectory="DgmlTpl" targetDirectory="Generated\Dgml" />

This producer instance executes all templates placed in the DgmlTpl directory, and places output files in the Generated\Dgml directory.

Of course you could do all that using other templating languages such as T4, however using the Template Producer allows you to work with the CodeFluent Entities API and consequently benefit from the meta-model (Project.Entities, Project.Relations, etc.) rather than parse XML.

Want to know more? You’ll find more information on the Template Producer in this documentation article.

Categories: Templates Tags: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s