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);
        }
    }
}

 

posted on 2018-06-22 19:00  chester·chen  阅读(256)  评论(0编辑  收藏  举报