以前在 “使用T4模板生成代码 – 初探” 文章简单的使用了T4模板的生成功能,但对于一个模板生成多个实例文件,如何实现这个方式呢?无心发现一个解决方案 “MultipleOutputHelper.ttinclude” ,它让基于T4模板批量生成文件实例变得简单起来了。git
Damien Guard是一个在加利福尼亚州圣何塞的开发人员,他构建出处理使用T4模板输出多文件的解决方案“MultipleOutputHelper.ttinclude”github
1. 初始化app
获取MultipleOutputHelper.ttinclude文件模板ide
注意: 文件能够上github.com 托管上面获取( https://github.com/damieng/DamienGKit/tree/master/T4/MultipleOutputHelper)spa
在T4模板中使用include指令导入MultipleOutputHelper.ttinclude文件或者将MultipleOutputHelper.ttinclude的代码复制在T4模板内。hibernate
而后初始化Manager对象,代码以下:debug
注意: 这里的Manager.ttinclude 就是MultipleOutputHelper.ttinclude文件模板code
2. 文件块对象
使用代码标识区分生成的代码块的范围blog
该代码声明了一个Employee.generated.cs文件,文件代码内容为:
1
|
public
class
Employee { ... }
|
3. 页眉和页脚
不少模板须要共享一个共同的页眉和页脚时,能够使用import语句进行打开和关闭。简单的使用StartHeader和StartFooter的代码方法进行分割。
4. 编译执行
使用Process方法,进行文件分割。
基于以前的“使用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); #>
output result:
输出文件效果:
link:
https://msdn.microsoft.com/zh-cn/library/bb126445.aspx