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

根据tt文件模板自动生成代码,下面依赖一个edmx,生成代码文件

<#@ 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\\DataModel.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 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().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;
        } 
	}
<#}#>
}

生成的文件如下

 
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,IOrderInfoService //crud
    {
		public override void SetCurrentDal()
        {
            CurrentDal = DbSession.OrderInfoDal;
        } 
	}
	
	public partial class TeacherService:BaseService,ITeacherService //crud
    {
		public override void SetCurrentDal()
        {
            CurrentDal = DbSession.TeacherDal;
        } 
	}
	
	public partial class UserInfoService:BaseService,IUserInfoService //crud
    {
		public override void SetCurrentDal()
        {
            CurrentDal = DbSession.UserInfoDal;
        } 
	}
}
这样我们在edmx中添加一个实体,模板自动给我们生成相应的业务

你可能感兴趣的:(mvc)