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

该博客介绍如何利用tt文件模板结合edmx,自动生成代码文件,简化开发流程。
部署运行你感兴趣的模型镜像

根据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中添加一个实体,模板自动给我们生成相应的业务

您可能感兴趣的与本文相关的镜像

Linly-Talker

Linly-Talker

AI应用

Linly-Talker是一款创新的数字人对话系统,它融合了最新的人工智能技术,包括大型语言模型(LLM)、自动语音识别(ASR)、文本到语音转换(TTS)和语音克隆技术

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值