zoukankan      html  css  js  c++  java
  • 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)inDebugBbing.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.DomaininDebugBbing.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);
            }
        }
    }
  • 相关阅读:
    jQuery基础之让出$,与其他库共存
    什么是闭包
    绑定repeater时三目运算加特殊结果处理
    将同一张表出来的两部分内容再合成一张表
    后台往前台写弹窗代码不显示
    固定行列转换加分段统计
    js调用后台方法(如果你能容忍执行的后台方法变成一个常量)
    javascript遍历数组
    基于SpringMVC框架使用ECharts3.0实现折线图,柱状图,饼状图,的绘制(上篇)
    echarts
  • 原文地址:https://www.cnblogs.com/chenyishi/p/9215140.html
Copyright © 2011-2022 走看看