T4,即4个T开头的英文字母组合:Text Template Transformation Toolkit。即一种自定义规则的代码生成器。根据业务模型可生成任何形式的文本文件或供程序调用的字符串。
利用T4模板,方便的从数据库映射成的Model模型中自动生成多层架构中结构相同的重复代码,避免重复手写。以后需要增加model的话,T4模板能追加相应代码。
IDAL层
<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF.Utility.CS.ttinclude"#>
<#@ output extension=".cs"#>
<#
CodeGenerationTools code = new CodeGenerationTools(this);
MetadataLoader loader = new MetadataLoader(this);
CodeRegion region = new CodeRegion(this, 1);
MetadataTools ef = new MetadataTools(this);
string inputFile = @"..\\Weick.Learn.Model\\WeickLearnModel.edmx";
EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
string namespaceName = code.VsNamespaceSuggestion();
EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);
#>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Weick.Learn.Model;
namespace Weick.Learn.IDAL
{<#
// Emit Entity Types
foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
{
//fileManager.StartNewFile(entity.Name + "RepositoryExt.cs");
//BeginNamespace(namespaceName, code);#>
public partial interface I<#=entity.Name#>Dal :IBaseDal<<#=entity.Name#>>
{
}
<#}#>
}
DAL层
<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF.Utility.CS.ttinclude"#>
<#@ output extension=".cs"#>
<#
CodeGenerationTools code = new CodeGenerationTools(this);
MetadataLoader loader = new MetadataLoader(this);
CodeRegion region = new CodeRegion(this, 1);
MetadataTools ef = new MetadataTools(this);
string inputFile = @"..\\Weick.Learn.Model\\WeickLearnModel.edmx";
EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
string namespaceName = code.VsNamespaceSuggestion();
EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);
#>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Weick.Learn.IDAL;
using Weick.Learn.Model;
namespace Weick.Learn.DAL
{<#
// Emit Entity Types
foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
{
//fileManager.StartNewFile(entity.Name + "RepositoryExt.cs");
//BeginNamespace(namespaceName, code);
#>
public partial class <#=entity.Name#>Dal :BaseDal<<#=entity.Name#>>,I<#=entity.Name#>Dal
{
}
<#}#>
}
DALFactory层
<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF.Utility.CS.ttinclude"#>
<#@ output extension=".cs"#>
<#
CodeGenerationTools code = new CodeGenerationTools(this);
MetadataLoader loader = new MetadataLoader(this);
CodeRegion region = new CodeRegion(this, 1);
MetadataTools ef = new MetadataTools(this);
string inputFile =@"..\\Weick.Learn.Model\\WeickLearnModel.edmx";
EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
string namespaceName = code.VsNamespaceSuggestion();
EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);
#>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using Weick.Learn.IDAL;
namespace Weick.Learn.DALFactory
{
public partial class AbstractFactory
{
<#
foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
{
#>
public static I<#=entity.Name#>Dal Create<#=entity.Name#>Dal()
{
string fullClassName = NameSpace + ".<#=entity.Name#>Dal";
return CreateInstance(fullClassName) as I<#=entity.Name#>Dal;
}
<#}#>
}
}
<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF.Utility.CS.ttinclude"#>
<#@ output extension=".cs"#>
<#
CodeGenerationTools code = new CodeGenerationTools(this);
MetadataLoader loader = new MetadataLoader(this);
CodeRegion region = new CodeRegion(this, 1);
MetadataTools ef = new MetadataTools(this);
string inputFile = @"..\\Weick.Learn.Model\\WeickLearnModel.edmx";
EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
string namespaceName = code.VsNamespaceSuggestion();
EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);
#>
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Weick.Learn.DAL;
using Weick.Learn.IDAL;
namespace Weick.Learn.DALFactory
{
public partial class DBSession : IDBSession
{
<#
// Emit Entity Types
foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
{
//fileManager.StartNewFile(entity.Name + "RepositoryExt.cs");
//BeginNamespace(namespaceName, code);
#>
private I<#=entity.Name#>Dal _<#=entity.Name#>Dal;
public I<#=entity.Name#>Dal <#=entity.Name#>Dal
{
get
{
if(_<#=entity.Name#>Dal == null)
{
_<#=entity.Name#>Dal = AbstractFactory.Create<#=entity.Name#>Dal();
}
return _<#=entity.Name#>Dal;
}
set { _<#=entity.Name#>Dal = value; }
}
<#}#>
}
}
IBLL层
<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF.Utility.CS.ttinclude"#>
<#@ output extension=".cs"#>
<#
CodeGenerationTools code = new CodeGenerationTools(this);
MetadataLoader loader = new MetadataLoader(this);
CodeRegion region = new CodeRegion(this, 1);
MetadataTools ef = new MetadataTools(this);
string inputFile = @"..\\Weick.Learn.Model\\WeickLearnModel.edmx";
EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
string namespaceName = code.VsNamespaceSuggestion();
EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);
#>
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Weick.Learn.Model;
namespace Weick.Learn.IBLL
{
<#
// Emit Entity Types
foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
{
//fileManager.StartNewFile(entity.Name + "RepositoryExt.cs");
//BeginNamespace(namespaceName, code);
#>
public partial interface I<#=entity.Name#>Service : IBaseService<<#=entity.Name#>>
{
}
<#}#>
}
BLL层
<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF.Utility.CS.ttinclude"#>
<#@ output extension=".cs"#>
<#
CodeGenerationTools code = new CodeGenerationTools(this);
MetadataLoader loader = new MetadataLoader(this);
CodeRegion region = new CodeRegion(this, 1);
MetadataTools ef = new MetadataTools(this);
string inputFile = @"..\\Weick.Learn.Model\\WeickLearnModel.edmx";
EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
string namespaceName = code.VsNamespaceSuggestion();
EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);
#>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Weick.Learn.IBLL;
using Weick.Learn.Model;
namespace Weick.Learn.BLL
{
<#
// Emit Entity Types
foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
{
//fileManager.StartNewFile(entity.Name + "RepositoryExt.cs");
//BeginNamespace(namespaceName, code);
#>
public partial class <#=entity.Name#>Service :BaseService<<#=entity.Name#>>,I<#=entity.Name#>Service
{
public override void SetCurrentDal()
{
CurrentDal = this.CurrentDBSession.<#=entity.Name#>Dal;
}
}
<#}#>
}
有关T4模板介绍
https://blog.csdn.net/shuai7boy/article/details/53203294?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase
https://blog.csdn.net/linjcai/article/details/81263215