【Reship】use of tangible T4 template engine

1.first of all

之前在 “使用T4模板生成代码 – 初探” 文章简单的使用了T4模板的生成功能,但对于一个模板生成多个实例文件,如何实现这个方式呢?无意发现一个解决方案 “MultipleOutputHelper.ttinclude” ,它让基于T4模板批量生成文件实例变得简单起来了。

 什么是MultipleOutputHelper.ttinclude

Damien Guard是一个在加利福尼亚州圣何塞的开发人员,他构建出处理使用T4模板输出多文件的解决方案“MultipleOutputHelper.ttinclude”

2.使用

1. 初始化

获取MultipleOutputHelper.ttinclude文件模板

注意: 文件可以上github.com 托管上面获取( https://github.com/damieng/DamienGKit/tree/master/T4/MultipleOutputHelper

在T4模板中使用include指令导入MultipleOutputHelper.ttinclude文件或者将MultipleOutputHelper.ttinclude的代码复制在T4模板内。

然后初始化Manager对象,代码如下:

 注意: 这里的Manager.ttinclude 就是MultipleOutputHelper.ttinclude文件模板

 

2. 文件块

使用代码标识区分生成的代码块的范围

该代码声明了一个Employee.generated.cs文件,文件代码内容为:

1
public class Employee { ... }

 

3. 页眉和页脚

很多模板需要共享一个共同的页眉和页脚时,可以使用import语句进行打开和关闭。简单的使用StartHeader和StartFooter的代码方法进行分割。

4. 编译执行

使用Process方法,进行文件分割。

3.场景应用

基于之前的“使用T4模板生成代码 – 初探” 文章的场景,进行基于NHibernate Mapper 来获取Domain对象,然后进行批量生成多个代码文件。

1. 自定义T4模板,文件名为“EntityRepositoryTemplate.tt”,代码如下:

<#@ template  language="C#"    debug="true" hostspecific="True"#> 

//  导入MultipleOutputHelper.ttinclude文件

<#@include file="$(SolutionDir)appT4MultipleOutputHelper.ttinclude"#> 

// 导入相关的DLL

<#@ Assembly Name="$(SolutionDir)libSharpArch.2.0.2NHibernate.dll" #>

<#@ Assembly Name="$(SolutionDir)libSharpArch.2.0.2SharpArch.NHibernate.dll" #>

<#@ Assembly Name="$(SolutionDir)libSharpArch.2.0.2SharpArch.Domain.dll" #>

<#@ Assembly Name="$(SolutionDir)libSharpArch.2.0.2FluentNHibernate.dll" #>

<#@ Assembly Name="$(SolutionDir)appCotide.Databin$(ConfigurationName)Cotide.Infrastructure.dll" #>

<#@ Assembly Name="$(SolutionDir)appCotide.Corebin$(ConfigurationName)Cotide.Domain.dll" #>

<#@ Assembly Name="$(SolutionDir)appCotide.Frameworkbin$(ConfigurationName)Cotide.Framework.dll" #>

<#@ import namespace="System.IO"#>

<#@ import namespace="System"#>

<#@ import namespace="System.Configuration"#>   

 <#  

    // 初始化

    SharpArch.NHibernate.NHibernateSession.CloseAllSessions();

    SharpArch.NHibernate.NHibernateSession.Reset();

    string projectPath = @"C:资料Person项目Codeplex电子商务app";

    string nhibernatePath = projectPath + @"Cotide.WebNHibernate.config";

    string[] mappingAssemblies = new[] { @"C:资料Person项目Codeplex电子商务appCotide.DatabinReleaseCotide.Infrastructure.dll" };

    // 加载配置

    NHibernate.Cfg.Configuration configuration = SharpArch.NHibernate.NHibernateSession.Init(

    new SharpArch.NHibernate.SimpleSessionStorage(), mappingAssemblies,

    new Cotide.Infrastructure.NHibernateMaps.AutoPersistenceModelGenerator().Generate(),

    nhibernatePath);

    // 获取所有类映射

    var allClassMetadata = SharpArch.NHibernate.NHibernateSession.GetDefaultSessionFactory().GetAllClassMetadata();

    var manager = Manager.Create(Host, GenerationEnvironment);  

    foreach (var entry in allClassMetadata)

    {

       var entityName = entry.Value.EntityName.Split('.'); 

       var className = entityName[entityName.Length - 1];

        // 定义输出文件

        manager.StartNewFile("I"+className+"Repository.cs");

       #>//-------------------------------------------------------------------

//版权所有:版权所有(C) 2012,Microsoft(China) Co.,LTD

//系统名称: 

//文件名称:I<#=className#>Repository.cs

//模块名称:

//模块编号:

//作  者:xcli

//创建时间:2013/4/6 12:49:50 

//功能说明:

//-----------------------------------------------------------------

//修改记录: 

//修改人:   

//修改时间: 

//修改内容: 

//-----------------------------------------------------------------  

using System;

using System.Collections.Generic;

using System.Linq; 

using System.Text;

using Cotide.Domain.Contracts.Repositories.Extension; 

namespace Cotide.Domain.Contracts.Repositories 

{ 

        public interface I<#=className#>Repository : IDbProxyRepository<<#=className#>>

        {


        } 

}

   <# 

       // 结束输出文件

       manager.EndBlock();

    } 

       // 执行编译

       manager.Process(true);  

    #> 
View Code

output result:

输出文件效果:

Others:

link:

https://msdn.microsoft.com/zh-cn/library/bb126445.aspx

 

posted @ 2015-11-28 22:47  calochCN  阅读(351)  评论(0编辑  收藏  举报