T4模板

https://www.cnblogs.com/ideacore/p/7803606.html

1.安装T4 Toolbox

 

2.新建T4 Toolbox模板

3.编辑T4 Toolbox模板 Bbing_Template_Mongo_Service.tt

<#+
// <copyright file="Bbing_Template_Repository.tt" company="">
//  Copyright © . All Rights Reserved.
// </copyright>

public class Bbing_Template_Mongo_Service : CSharpTemplate
{
    private string _className;

    public Bbing_Template_Mongo_Service(string className)
    {
        _className = className;
    }

    public override string TransformText()
    {
        base.TransformText();
#>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bbing.Domain.IRepositories;
using Bbing.Domain.Model;
using Bbing.Domain.Service;

namespace Bbing.Service.Services.Mongo
{
    public partial class  <#= _className #>Service:MongoBaseService< <#= _className #>>,I<#= _className #>Service 
    {
        public <#= _className #>Service(I<#= _className #>Repository repository)
        {
            SetCurrentRepository(repository);
        }
    }
}
<#+
        return this.GenerationEnvironment.ToString();
    }
}
#>

4.添加T4模板 Bbing_Template.tt

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Reflection" #>
<#@ import namespace="System.Diagnostics" #>
<#@ import namespace="System.IO" #>
<#@ assembly name="$(ProjectDir)\bin\Debug\Bbing.Domain.dll" #>
<#@ import namespace="Bbing.Domain.BaseModel" #>
<#@ include file="T4Toolbox.tt" #>
<#@ include file="Bbing_Template_Mongo_Service.tt" #>
<#@ output extension=".cs" #>

<#
    string solutionsPath = Host.ResolveAssemblyReference("$(SolutionDir)");  
    string projectPath = Host.ResolveAssemblyReference("$(ProjectDir)");  
    string destPath_Repository = Path.Combine(projectPath, "Services/Mongo/t4");
#>

<#
    var types = Assembly.LoadFrom(solutionsPath + @"\Bbing.Domain\bin\Debug\Bbing.Domain.dll").GetTypes();
    foreach (var item in types)
    {
        string className = item.Name;
        if(item.GetInterface("IAggregateRoot") != null && className != "AggregateRoot")
        {
            Bbing_Template_Mongo_Service template = new Bbing_Template_Mongo_Service(className);
            string fileName = string.Format(@"{0}\{1}.cs", destPath_Repository, $"{className}Service");
            template.Output.Encoding = Encoding.UTF8;
            template.RenderToFile(fileName);
        }
    }
#>

 5.可看到生成的文件

 6.内容为

// <autogenerated>
//   This file was generated by T4 code generator Bbing_Template.tt.
//   Any changes made to this file manually will be lost next time the file is regenerated.
// </autogenerated>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bbing.Domain.IRepositories;
using Bbing.Domain.Model;
using Bbing.Domain.Service;

namespace Bbing.Service.Services.Mongo
{
    public partial class  TB_MENUService:MongoBaseService< TB_MENU>,ITB_MENUService 
    {
        public TB_MENUService(ITB_MENURepository repository)
        {
            SetCurrentRepository(repository);
        }
    }
}

 

转载于:https://www.cnblogs.com/chenyishi/p/9215140.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值