FANDOM


Create an entity class templateEdit

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


StepsEdit

  1. Add a .TT file
  2. Copy the code from the Example



Code ExplanationEdit

T4 Code ExampleEdit

<# 
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" #>
<#
this.EntityPropertyRenderers.Add(new RenderParentOne());
this.EntityPropertyRenderers.Add(new RenderParentMany());
this.EntityPropertyRenderers.Add(new RenderKeyProperty());
this.EntityPropertyRenderers.Add(new RenderProperty());

foreach(Entity entity in Entities)
{
StartNewFile(entity.Name + "Entity.cs");
WriteAutoGeneratedComment(version);
#> 
using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using Cirrious.MvvmCross.Plugins.Sqlite;
using <#=Namespace#>.Core.Dal; 
using TeaLightV5.Foundation.BL;
using TeaLightV5.Foundation.Sal.Interfaces;

amespace <#=Namespace#>.Core.BL.Entities
{
public partial class <#=entity.Name#>Entity : Entity, ISyncableEntity
{
        #region Constructor

       public <#=entity.Name#>Entity()
        {

<#
foreach(Property property in entity.Properties)
{
if (!string.IsNullOrEmpty(property.DefaultString))
                {

#>
<#=property.Name#> = <#=property.DefaultString#>; 
<#
                }
}
#>
        } 
        #endregion

        #region Fields
<#
RenderEntityProperties(entity);
#> #endregion
}
}
<#
}
ProcessFiles();
#>

<#+
public class RenderKeyProperty : T4UtilityBelt.EntityModels3.Renderers.Renderer<T4UtilityBelt.EntityModels3.KeyProperty>
{
public override void Render(T4UtilityBelt.EntityModels3.KeyProperty property)
    {
    }
}
#>
<#+
public class RenderProperty : T4UtilityBelt.EntityModels3.Renderers.Renderer<T4UtilityBelt.EntityModels3.Property>
{
public override void Render(T4UtilityBelt.EntityModels3.Property property)
    {
       string propertyName = property.Name;
       string propertyType = property.PropertyType;
#> [XmlElement("<#=propertyName#>")]
        public <#=propertyType#> <#=propertyName#> { get; set; }
<#+
    }
}
#>
<#+
public class RenderParentOne : T4UtilityBelt.EntityModels3.Renderers.Renderer<T4UtilityBelt.EntityModels3.ParentOne>
{
public override void Render(T4UtilityBelt.EntityModels3.ParentOne property)
    {
string propertyType = property.PropertyType;
if (property.PropertyType == "Guid"
|| property.PropertyType == "Guid?")
        {
propertyType = "string";
        }
#> [XmlElement("<#=property.Name#>Entity")]
        public <#=propertyType#> <#=property.Name#> { get; set; }
[XmlIgnore]
        [Ignore]
        public <#=property.ToName#>Entity <#=property.Association#> 
        {
            get
            {
                return <#=property.Entity.Name#>Repository.Get<#=property.ToName#>(this);
            }
        } 
<#+
    }
}
#>
<#+
public class RenderParentMany : T4UtilityBelt.EntityModels3.Renderers.Renderer<T4UtilityBelt.EntityModels3.ParentMany>
{
public override void Render(T4UtilityBelt.EntityModels3.ParentMany property)
    {
#> [XmlIgnore]
        [Ignore]
        public IEnumerable<<#=property.PropertyType#>> <#=property.ToEntity.PluralName#>
        {
            get
            {
                return <#=property.Entity.Name#>Repository.Get<#=property.ToEntity.PluralName#>(this);
            }
        } 
<#+
    }
}
#>

 


Generated Code ExampleEdit

using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using Cirrious.MvvmCross.Plugins.Sqlite;
using Conference.Core.Dal; 
using TeaLightV5.Foundation.BL;
using TeaLightV5.Foundation.Sal.Interfaces;

namespace Conference.Core.BL.Entities
{
    public partial class SessionEntity : Entity, ISyncableEntity
    {
        #region Constructor

        public SessionEntity()
        {
        } 

        #endregion


        #region Fields

        [XmlElement("Created")]
        public DateTime Created { get; set; }

        [XmlElement("CreatedBy")]
        public string CreatedBy { get; set; }

        [XmlElement("Modified")]
        public DateTime Modified { get; set; }

        [XmlElement("ModifiedBy")]
        public string ModifiedBy { get; set; }

        [XmlElement("Name")]
        public string Name { get; set; }

        #endregion

    }
}

 

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.