根据tt文件模板自动生成代码,下面依赖一个edmx,生成代码文件
<span style="font-size:12px;color:#000000;"><#@ 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 = @"..\\Company.OA.Model\\<span style="color:#CC0000;">DataModel.edmx</span>";
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 Company.OA.DALFactory;
using Company.OA.IBLL;
using Company.OA.IDAL;
using Company.OA.Model;
namespace Company.OA.BLL
{
<#
foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
{
#>
public partial class <#=entity.Name#>Service:BaseService<<#=entity.Name#>>,I<#=entity.Name#>Service //crud
{
public override void SetCurrentDal()
{
CurrentDal = DbSession.<#=entity.Name#>Dal;
}
}
<#}#>
}</span>生成的文件如下
<span style="font-size:12px;color:#000000;">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Company.OA.DALFactory;
using Company.OA.IBLL;
using Company.OA.IDAL;
using Company.OA.Model;
namespace Company.OA.BLL
{
public partial class OrderInfoService:BaseService<OrderInfo>,IOrderInfoService //crud
{
public override void SetCurrentDal()
{
CurrentDal = DbSession.OrderInfoDal;
}
}
public partial class TeacherService:BaseService<Teacher>,ITeacherService //crud
{
public override void SetCurrentDal()
{
CurrentDal = DbSession.TeacherDal;
}
}
public partial class UserInfoService:BaseService<UserInfo>,IUserInfoService //crud
{
public override void SetCurrentDal()
{
CurrentDal = DbSession.UserInfoDal;
}
}
}</span>这样我们在edmx中添加一个实体,模板自动给我们生成相应的业务
该博客介绍如何利用tt文件模板结合edmx,自动生成代码文件,简化开发流程。
6099





