Generating multiple files from one T4 template

In the previous posts about T4 I firstly drove T4 generation from EF entity definitions, then used this to make EF POCO classes with certain properties implement an interface. Please read these posts before reading this one – in particular the code in this post refers to code from the previous one.

In this post, I’ll extend what I’ve already built to handle multiple interfaces, and to generate a single file per interface.

For this example, I’m going to use two interfaces.

public interface IUserNameStamped
{
    string UserName { get; set; }
}

public interface ILookup
{
    string Code { get; set; }

    string Description { get; set; }
}

I want my EF POCOs to implement IUserNameStamped if they have a UserName property, and ILookup if they have a Code and Description property. I want the IUserNameStamped code in a file IUserNameStamped.cs, and ILookup code in a file ILookup.cs.

By default, a T4 template will generate a single file with the same name as the the template, and the extension defined by the <#@ output #> directive. The EntityFrameworkTemplateFileManager, used by EF to generate a file per entity, is the secret to generating multiple files from a single template.

The other change needed to the T4 code we already have is to break it into reusable methods that can be shared for each entity.

The method I’ve defined to generate an file for a given interface is CreateInterfaceFile, shown here with support classes.

<#+
void CreateInterfaceFile(EntityFrameworkTemplateFileManager fileManager,  
	CodeGenerationTools code,
	EdmItemCollection itemCollection,
	string namespaceName, 
	Action interfaceWriter, 
	string interfaceName, 
	params string[] requiredProperties)
{
    fileManager.StartNewFile(interfaceName + ".cs");
	BeginNamespace(namespaceName, code);
	interfaceWriter();
	var entities = GetEntitiesWithPropertyOrRelationship(itemCollection,
		requiredProperties);
	foreach (EntityType entity in entities.OrderBy(e => e.Name))
	{
		WriteInterfaceImplementation(entity.Name, interfaceName);
	}
	EndNamespace(namespaceName);
}
#>
<#+
void BeginNamespace(string namespaceName, CodeGenerationTools code)
{
    CodeRegion region = new CodeRegion(this);
    if (!String.IsNullOrEmpty(namespaceName))
    {
#>
namespace <#=code.EscapeNamespace(namespaceName)#>
{
<#+
        PushIndent(CodeRegion.GetIndent(1));
    }
}


void EndNamespace(string namespaceName)
{
    if (!String.IsNullOrEmpty(namespaceName))
    {
        PopIndent();
#>
}
<#+
    }
}

IEnumerable<EntityType> GetEntitiesWithPropertyOrRelationship(
	EdmItemCollection itemCollection, 
	params string[] requiredProperties)
{
	return itemCollection.GetItems<EntityType>().Where(entity => 
		EntityHasPropertyOrRelationship(entity, requiredProperties));
}

bool EntityHasPropertyOrRelationship
	(EntityType entity, params string[] requiredProperties)
{
	return requiredProperties.All(
		requiredProperty => entity.Properties.Any(property => property.Name == requiredProperty)
		|| entity.NavigationProperties.Any(property => property.Name == requiredProperty));
}

void WriteInterfaceImplementation(string entityName, string interfaceName)
{
#>

public partial class <#=entityName#> : <#=interfaceName#>
{
}
<#+
}

The parameters of CreateInterfaceFile:

  • The first three parameters are T4 and EF classes instantiated at the top of the template and passed in.
  • namespaceName is also provided by T4 – the namespace the interface and classes will belong to.
  • interfaceWriter is a action that writes out the definition of the interface itself.
  • interfaceName is the name of the interface.
  • requiredProperties is an array of all the properties a class must have to be considered to implement the interface.

The logic is very simple

  • The EntityFrameworkTemplateFileManager is used to start a file for the interface – all output now goes to this file until the next time StartNewFile is called.
  • The namespace is written.
  • The declaration of the interface is written.
  • Entities matching this interface are found using GetEntitiesWithPropertyOrRelationship (as explained in the previous blog post.
  • An partial implementation of the class for each matching entity is written, with no content, simply stating that the class implements the interface in question.
  • The namespace is closed.

That’s about all there is to it. Once again, an extension to this code to match entity properties by type as well as name is left as an exercise to the reader.

Here is full source code:

<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF.Utility.CS.ttinclude"#><#@
 output extension=".cs"#><#

string inputFile = @"OticrsEntities.edmx";
EdmItemCollection itemCollection = new MetadataLoader(this).
	CreateEdmItemCollection(inputFile);

CodeGenerationTools code = new CodeGenerationTools(this);
string namespaceName = code.VsNamespaceSuggestion();

EntityFrameworkTemplateFileManager fileManager = 
	EntityFrameworkTemplateFileManager.Create(this);
WriteHeader(fileManager);

#>
// Default file generated by T4. Generation cannot be prevented. Please ignore.
<#

CreateInterfaceFile(fileManager, 
	code,
	itemCollection, 
	namespaceName,
	WriteILookupInterface,
	"ILookup",
	"ContractorCode", "Description");

CreateInterfaceFile(fileManager, 
	code,
	itemCollection, 
	namespaceName,
	WriteIUserNameStampedInterface,
	"IUserNameStamped",
	"UserName");
	
fileManager.Process(true);

#>
<#+
void CreateInterfaceFile(EntityFrameworkTemplateFileManager fileManager,  
	CodeGenerationTools code,
	EdmItemCollection itemCollection,
	string namespaceName, 
	Action interfaceWriter, 
	string interfaceName, 
	params string[] requiredProperties)
{
    fileManager.StartNewFile(interfaceName + ".cs");
	BeginNamespace(namespaceName, code);
	interfaceWriter();
	var entities = GetEntitiesWithPropertyOrRelationship(itemCollection, 
		requiredProperties);
	foreach (EntityType entity in entities.OrderBy(e => e.Name))
	{
		WriteInterfaceImplementation(entity.Name, interfaceName);
	}
	EndNamespace(namespaceName);
}
#>
<#+
void WriteHeader(EntityFrameworkTemplateFileManager fileManager, 
	params string[] extraUsings)
{
    fileManager.StartHeader();
#>
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Collections.Generic;

<#=String.Join(String.Empty, extraUsings.
		Select(u => "using " + u + ";" + Environment.NewLine).
		ToArray())#>
<#+
    fileManager.EndBlock();
}

void BeginNamespace(string namespaceName, CodeGenerationTools code)
{
    CodeRegion region = new CodeRegion(this);
    if (!String.IsNullOrEmpty(namespaceName))
    {
#>
namespace <#=code.EscapeNamespace(namespaceName)#>
{
<#+
        PushIndent(CodeRegion.GetIndent(1));
    }
}


void EndNamespace(string namespaceName)
{
    if (!String.IsNullOrEmpty(namespaceName))
    {
        PopIndent();
#>
}
<#+
    }
}

IEnumerable<EntityType> GetEntitiesWithPropertyOrRelationship(
	EdmItemCollection itemCollection, 
	params string[] requiredProperties)
{
	return itemCollection.GetItems<EntityType>().Where(entity => 
		EntityHasPropertyOrRelationship(entity, requiredProperties));
}

bool EntityHasPropertyOrRelationship(EntityType entity, 
	params string[] requiredProperties)
{
	return requiredProperties.All(requiredProperty => 
		entity.Properties.Any(property => property.Name == requiredProperty)
		|| entity.NavigationProperties.Any(property => property.Name == requiredProperty));
}

void WriteInterfaceImplementation(string entityName, string interfaceName)
{
#>

public partial class <#=entityName#> : <#=interfaceName#>
{
}
<#+
}

void WriteILookupInterface()
{
#>
/// <summary>
/// A lookup entity, that can be looked up by a ContractorCode
/// </summary>
public interface ILookup
{
    string ContractorCode { get; set; }
	
	string Description { get; set; }
}
<#+
}

void WriteIUserNameStampedInterface()
{
#>
/// <summary>
/// An entity that is stamped with the Username that created it
/// </summary>
public interface IUserNameStamped
{
    string UserName { get; set; }
}
<#+
}
#>

Duck typing Entity Framework classes using T4 Templates

Duck typing is an interesting concept, and alien to C# generally. But using the techniques of my previous post about T4 and Entity Framework, it is possible to have your entities implement interfaces if they have the required properties, resulting in behaviour similar to duck typing. Please read the previous blog post before reading this one.

The previous blog post gives us code to implement interfaces for each entity in an object model. In order to provide “duck typing”, we will extend this to only implement the interface for an entity if that entity has the properties of the interface.

Fortunately System.Data.Metadata.Edm.EntityType gives us the ability to inspect the properties of an entity. For my purposes, I only check for properties by name, as I control my database and would never have the same column name with two different data types. Extension of this code to check property types as well as names is left as an exercise for the reader.

IEnumerable<EntityType> GetEntitiesWithPropertyOrRelationship(
    EdmItemCollection itemCollection, params string[] requiredProperties)
{
    return itemCollection.GetItems<EntityType>().
        Where(entity => EntityHasPropertyOrRelationship(entity, requiredProperties));
}

bool EntityHasPropertyOrRelationship(
    EntityType entity, params string[] requiredProperties)
{
    return requiredProperties.All(
        requiredProperty => entity.Properties.Any(property => property.Name == requiredProperty)
        || entity.NavigationProperties.Any(property => property.Name == requiredProperty));
}

Pretty simple stuff. EntityHasPropertyOrRelationship checks both the Properties (properties relating to simply database columns), and NavigationProperties (properties relating to foreign key relationships) for properties with the required names. If our entity has all the required properties, it’s a match.

GetEntitiesWithPropertyOrRelationship uses EntityHasPropertyOrRelationship to retrieve all the entities that have the required properies from our itemCollection.

I’ve blogged about further extending the template to handle multiple interfaces, with one file per interface.

Here’s the full code of the example from the last blog post, updated so entities only implement IUserNameStamped if they actually have a column called UserName.

<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF.Utility.CS.ttinclude"#>
<#@ output extension=".cs"#>
<#
string inputFile = @"Entities.edmx";
EdmItemCollection itemCollection = new MetadataLoader(this).
    CreateEdmItemCollection(inputFile);

CodeGenerationTools code = new CodeGenerationTools(this);
string namespaceName = code.VsNamespaceSuggestion();

WriteHeader();
BeginNamespace(namespaceName, code);
WriteIUserNameStamped();
WriteEntitiesWithInterface(itemCollection);
EndNamespace(namespaceName);
#>
<#+
void WriteHeader(params string[] extraUsings)
{
#>
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

<#=String.Join(String.Empty, extraUsings.
    Select(u => "using " + u + ";" + Environment.NewLine).ToArray())#>
<#+
}

void BeginNamespace(string namespaceName, CodeGenerationTools code)
{
    CodeRegion region = new CodeRegion(this);
    if (!String.IsNullOrEmpty(namespaceName))
    {
#>
namespace <#=code.EscapeNamespace(namespaceName)#>
{
<#+
        PushIndent(CodeRegion.GetIndent(1));
    }
}


void EndNamespace(string namespaceName)
{
    if (!String.IsNullOrEmpty(namespaceName))
    {
        PopIndent();
#>
}
<#+
    }
}

void WriteIUserNameStamped()
{
#>
/// <summary>
/// An entity that is stamped with the Username that created it
/// </summary>
/// <remarks>
/// All OTICRS entities should have a username. If any entity fails to implement
/// this interface, it means the table needs the UserName column added to it.
/// </remarks>
public interface IUserNameStamped
{
    string UserName { get; set; }
}

<#+
}

void WriteEntitiesWithInterface(
    EdmItemCollection itemCollection)
{
    foreach (EntityType entity in 
        GetEntitiesWithPropertyOrRelationship(itemCollection, "UserName").
        OrderBy(e => e.Name))
    {
        WriteEntityWithInterface(entity.Name);
    }
}

IEnumerable<EntityType> GetEntitiesWithPropertyOrRelationship(
    EdmItemCollection itemCollection, params string[] requiredProperties)
{
    return itemCollection.GetItems<EntityType>().Where(
        entity => EntityHasPropertyOrRelationship(entity, requiredProperties));
}

bool EntityHasPropertyOrRelationship(
    EntityType entity, params string[] requiredProperties)
{
    return requiredProperties.All(
        requiredProperty => entity.Properties.Any(property => property.Name == requiredProperty)
        || entity.NavigationProperties.All(property => property.Name == requiredProperty));
}

void WriteEntityWithInterface(string entityName)
{
#>
public partial class <#=entityName#> : IUserNameStamped
{
}

<#+
}

#>

Using T4 Templates to extend your Entity Framework classes

A set of entities I’m using with Entity Framework (I’m using EF POCO) have common properties implying commonality between the entities. I didn’t want to use any form of inheritance within my object model to express this commonality, but I did wish to have the entity classes implement common interfaces. It’s easy to do this because entities are partial classes. Say for example all my entities have a string property UserName, I can define an interface to express this, and then have a partial implemention of the class that implements the interface.

public interface IUserNameStamped
{
    string UserName { get; set; }
}
    
public partial class Entity1 : IUserNameStamped
{
}
    
public partial class Entity2 : IUserNameStamped
{
}

So the POCO T4 template generates the “main” class definition for each entity, with all it’s properties, and then these partial classes extend the class, not adding any new properties or methods, just extending with the fact each class implements the IUserNameStamped interface.

I quickly realised that I could use T4 in a similar manner to the EF POCO T4 template, in order to produce these partial classes automatically.

As I explained in my post about UserName stamping entities as they’re saved, all my entities have a UserName column. So all this template has to do is loop through all the entities in my object model, and write an implementation for each.

The main T4 logic is

<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF.Utility.CS.ttinclude"#>
<#@ output extension=".cs"#>
<#
string inputFile = @"OticrsEntities.edmx";
EdmItemCollection itemCollection = new MetadataLoader(this).
    CreateEdmItemCollection(inputFile);

CodeGenerationTools code = new CodeGenerationTools(this);
string namespaceName = code.VsNamespaceSuggestion();

WriteHeader();
BeginNamespace(namespaceName, code);
WriteIUserNameStamped();
WriteEntitiesWithInterface(itemCollection);
EndNamespace(namespaceName);
#>

Most of this is cribbed unashamedly from the EF POCO T4 template. Firstly we initialise some variables, the most interesting being itemCollection, which is what allows access to the entity metadata. We then write a header indicate the file is a generated file, start the namespace, write the actual declaration of the IUsernameStamped interface, write a partial class for each entity implementing the interface, and then end the namespace. The specifics of each method are:

<#+
void WriteHeader(params string[] extraUsings)
{
#>
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

<#=String.Join(String.Empty, 
    extraUsings.Select(u => "using " + u + ";" + Environment.NewLine).ToArray())#>
<#+
}

void BeginNamespace(string namespaceName, CodeGenerationTools code)
{
    CodeRegion region = new CodeRegion(this);
    if (!String.IsNullOrEmpty(namespaceName))
    {
#>
namespace <#=code.EscapeNamespace(namespaceName)#>
{
<#+
        PushIndent(CodeRegion.GetIndent(1));
    }
}


void EndNamespace(string namespaceName)
{
    if (!String.IsNullOrEmpty(namespaceName))
    {
        PopIndent();
#>
}
<#+
    }
}

I think these three methods are fairly self-explanatory, other than the <# syntax that T4 uses to indicate code and text blocks.

void WriteIUserNameStamped()
{
#>
/// <summary>
/// An entity that is stamped with the Username that created it
/// </summary>
public interface IUserNameStamped
{
    string UserName { get; set; }
}

<#+
}

Simply generates the interface definition.

void WriteEntitiesWithInterface(EdmItemCollection itemCollection)
{
	foreach (EntityType entity in itemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
	{
		WriteEntityWithInterface(entity.Name);
	}
}

Iterates through the entities.

void WriteEntityWithInterface(string entityName)
{
#>
public partial class <#=entityName#> : IUserNameStamped
{
}

<#+
}

#>

Writes an implementation of the IUserNameStamped interface for each entity.

So you can see it’s fairly simple to use T4 to generate C# code similar to that at the top of this blog post.

I’ve blogged about how I extended this code to make a certain set of entities with common properties implement a common interface

I’ve also blogged about further extending the template to handle multiple interfaces, with one file per interface.

This is the full code of the T4 template:

<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF.Utility.CS.ttinclude"#>
<#@ output extension=".cs"#>
<#
string inputFile = @"Entities.edmx";
EdmItemCollection itemCollection = new MetadataLoader(this).CreateEdmItemCollection(inputFile);

CodeGenerationTools code = new CodeGenerationTools(this);
string namespaceName = code.VsNamespaceSuggestion();

WriteHeader();
BeginNamespace(namespaceName, code);
WriteIUserNameStamped();
WriteEntitiesWithInterface(itemCollection);
EndNamespace(namespaceName);
#>
<#+
void WriteHeader(params string[] extraUsings)
{
#>
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

<#=String.Join(String.Empty, extraUsings.Select(u => "using " + u + ";" + Environment.NewLine).ToArray())#>
<#+
}

void BeginNamespace(string namespaceName, CodeGenerationTools code)
{
    CodeRegion region = new CodeRegion(this);
    if (!String.IsNullOrEmpty(namespaceName))
    {
#>
namespace <#=code.EscapeNamespace(namespaceName)#>
{
<#+
        PushIndent(CodeRegion.GetIndent(1));
    }
}


void EndNamespace(string namespaceName)
{
    if (!String.IsNullOrEmpty(namespaceName))
    {
        PopIndent();
#>
}
<#+
    }
}

void WriteIUserNameStamped()
{
#>
/// <summary>
/// An entity that is stamped with the Username that created it
/// </summary>
public interface IUserNameStamped
{
    string UserName { get; set; }
}

<#+
}

void WriteEntitiesWithInterface(EdmItemCollection itemCollection)
{
	foreach (EntityType entity in itemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
	{
		WriteEntityWithInterface(entity.Name);
	}
}

void WriteEntityWithInterface(string entityName)
{
#>
public partial class <#=entityName#> : IUserNameStamped
{
}

<#+
}

#>

Automatically username stamping entities as they’re saved

The application I am currently working on has a requirement to audit which application user last created or updated database records. All tables in the database are required to have an nvarchar column UserName.

I didn’t want this concern to leak into my application. After some investigation I discovered that ObjectContext has the SavingChanges event that would be ideal for my purposes.

So the creation of my ObjectContext becomes

var entities = new MyEntities();
entities.SavingChanges += SetUserNameEvent;

I originally thought that SetUserNameEvent would have to use reflection to obtain and set the UserName property. However I found a way to use T4 to generate code resulting in all entities with the UserName property implementing a common interface (IUserNameStamped). I’ve written a blog post talking about about the T4 code.

So with all my entities implementing this common interface, SetUserNameEvent is then

/// <summary>
/// Sets the user name of all added and modified 
/// entities to the username provided by
/// the <see cref="UserNameProvider"/>. 
/// </summary>
private void SetUserNameEvent(object sender, EventArgs e)
{
    Contract.Requires<ArgumentException>(
        sender is ObjectContext, 
        "sender is not an instance of ObjectContext");
    var objectContext = (ObjectContext)sender;
    foreach (ObjectStateEntry entry in 
        objectContext.ObjectStateManager.GetObjectStateEntries(
            EntityState.Added | EntityState.Modified))
    {
        var stamped = entry.Entity as IUserNameStamped;
        Contract.Assert(stamped != null, 
            "Expected all entities implement IUserNameStamped");
        stamped.UserName = UserNameProvider.UserName;
    }
}

So here, we get all added and modified entries from the ObjectStateManager, and use these to obtain the entities and set their UserName. UserNameProvider is an abstraction used as I have several applications utilising my object context, each with a different way to obtain the current application user. Note that my code is using Code Contracts.

One complication I’ve found is with child entities. Sometimes, I’ve found I have to add the child entity both to its parent object and to the object context, but sometimes it’s enough to simply add the child entity to it’s parent object. That is:

var entities = ObjectContextFactory.GetObjectContext();
var childEntity = new ChildEntity();
entities.ParentEntities.First().ChildEntities.Add(childEntity);
// entities.ChildEntities.AddObject(childEntity);
entities.SaveChanges()
// Sometimes UserName will not get set without the commented line above, 
// resulting in a NOT NULL constraint violation

I’ve found no rhyme or reason as to why the addition to the ObjectContext is only sometimes required, I’d love hints as to why this is.

Note I’m actually using the unit of work pattern for my application, and I use a unit of work factory rather than an object context factory, but that’s irrelevant to the use of the SavingChanges event in this fashion.