根据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中添加一个实体,模板自动给我们生成相应的业务
<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中添加一个实体,模板自动给我们生成相应的业务
相关文章推荐
- 解密Java IO流中的flush()方法的由来
- java编写服务_客户端通信实例讲解(适合初学者)
- 魔兽世界终极版(C++程序设计第6周)
- mysql binlog系列(二)----java解析binlog
- 实战c++中的vector系列--vector应用之STL的find、find_if、find_end、find_first_of、find_if_not(C++11)
- 实战c++中的vector系列--vector应用之STL的find、find_if、find_end、find_first_of、find_if_not(C++11)
- python3打包成exe
- 多态与虚函数编程题 #3(C++程序设计第6周)
- 查找qt生成的exe软件需要的动态库.以及打包
- 多态与虚函数编程题 #2(C++程序设计第6周)
- Hadoop教程(四)HDFS文件系统JavaAPI接口
- java异常处理机制throw
- Java虚拟机 垃圾回收 之 引用计数
- java.lang.UnsatisfiedLinkError: Native method not found:问题解决
- Git源代码管理
- python 类
- JAVA MD5的误区
- Java集合类概述
- 多态与虚函数编程题 #1(C++程序设计第6周)
- ccf试题201312-2——ISBN号码