您的位置:首页 > 编程语言

根据tt文件模板自动生成代码

2015-12-22 23:43 671 查看
根据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中添加一个实体,模板自动给我们生成相应的业务
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: