TeaLight Wiki
Advertisement

Create a Collection class Template[]

Prerequisites[]

  1. Setup the T4 Development Environment
  2. Create a project 
  3. Add a T4 Entity Model


Steps

  1. Add a .TT file
  2. Copy the code as described below

Code Explanation[]

  1. Reference the base template: T4UtilityBelt.Templates3.Template
  2. Add a volatileAssembly reference to T4UtilityBelt
  3. Add the include file reference
  4. The code loops through the Entities collection and creates one file per entity. Each file has a collection class.


T4 Code Example[]

<# 
var version = "0.1";
#>
<#@ template language="C#" debug="true" hostspecific="true" 
inherits="T4UtilityBelt.Templates3.Template"  #>
<#@ output extension = ".txt" #>
<#@ VolatileAssembly Processor="VolatileAssemblyProcessor" 
name="T4UtilityBelt" #>
<#@ include file="$(SolutionDir)\TealightV5\Conference.Designs\Entities.ttinclude" #>
<#
foreach(Entity entity in Entities)
{
StartNewFile(entity.Name + "EntityCollection.cs");
WriteAutoGeneratedComment(version);
#> 
using System.Collections.ObjectModel;
using <#=Namespace#>.Core.BL.Entities;
namespace <#=Namespace#>.BL.Collections
{
   public partial class <#=entity.Name#>EntityCollection 
          : Collection<<#=entity.Name#>Entity>
   {
   }
}

<#
}
ProcessFiles();
#>



Generated Code Example[]

using System.Collections.ObjectModel;
using Conference.Core.BL.Entities;
namespace Conference.BL.Collections
{
     public partial class SessionEntityCollection : Collection<SessionEntity>
     {
     }
}

 

Advertisement