Home > Templates > CodeFluent Entities: Writing a Template Generating One File Per Entity

CodeFluent Entities: Writing a Template Generating One File Per Entity


Following the previous post “Generating Custom Code For All Entities” a colleague showed me the greatest way ever to iterate on a collection of concepts and generate text a file for each from a template, so I thought I’d share it Smile

In this post we’ll write a template to generate a C# file per entity extending the one generated by the Business Object Model producer. The generated file should look like this one:

namespace Sample
{
    public partial class Customer
    {
        public static Customer LoadFromLob(string entityKey)
        {
            // TODO: load from external LOB
        }
    
        public void SendToLob()
        {
            // TODO: save current instance to external LOB
        }
    }
}

 

To generate code like the one above I created a template file named “[Template]entity.tpl”.

Note: The “[Template]” prefix is important as it indicates the producer to run the template. Other than that, the rest of the name doesn’t matter as we’ll name our output files <EntityName>.g.cs and this will be done in the template.

Here’s my template:

[%@ template enumerable='Producer.Project.Entities' enumerableItemName="entity" enumerableTargetPathFunc='Path.Combine(Path.GetDirectoryName(TargetPath), entity.Name) + ".g.cs"' %]
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by the Template Producer.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace [%=entity.Namespace%]
{
    public partial class [%=entity.Name%]
    {
        public static [%=entity.Name%] LoadFromLob(string entityKey)
        {
            // TODO: load from external LOB
        }
    
        public void SendToLob()
        {
            // TODO: save current instance to external LOB
        }
    }
}

Instead of all the code which we used to have I now have a single line which will have the exact same effect. Here’s a description of what it does:

  • the enumerable attribute takes an IEnumerable as an input. In our case, I’ve set it to take as input all entities of my project. This is possible as all producers have a Project property corresponding to the model inferred from your model.
  • the enumerableItemName attribute corresponds to the item name which I named “entity” as I’m enumerating on all entities. That’s basically the variable name of my foreach, and that’s how I can use the “entity” variable in the rest of my template.
  • the enumerableTargetPathFunc is a code snippet to be called to compute the target path of the generated file. This is thanks to this snippet that I’m generating files named as “<EntityName>.g.cs”.

Running my new template, here’s the result:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by the Template Producer.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace Sample
{
    public partial class Customer
    {
        public static Customer LoadFromLob(string entityKey)
        {
            // TODO: load from external LOB
        }
    
        public void SendToLob()
        {
            // TODO: save current instance to external LOB
        }
    }
}

 

Carl Anderson

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