SuuchaT4Includes:一些T4模板的include文件(包含基于T4模板的asp.net mvc脚手架)

最近artech写了系列T4模板的文章,我也来凑凑热闹,发布3个T4模板的include文件,我把源代码发布到CodePlex上了,地址:http://t4includes.codeplex.com/

现在简单介绍一下这3个include,以后有时间再详细介绍 微笑

最基本的:VSHost.ttinclude

这个include就是提供一些简单地操作当前Solution(及所有Project)的方法:

静态方法:

  • public static ProjectItem FindContainer(ProjectItem item):查找给定的项目的父级,如果没有父级就返回自己。
  • public static Project FindProject(Solution solution, string projectName):根据项目名称在指定的解决方案中查找项目。
  • public static Project[] AllProjects(Solution solution):获取指定的解决方案中的所有项目。
  • public static string GetRelativePath(ProjectItem parent, ProjectItem child):获取给定的2个项目项的相对路径。
  • public static ProjectItem FindItem(ProjectItems items, string itemName, bool create):根据指定的项目项的名称和项目项列表查找项目项,这里的项目项可以是具体的文件(包括在项目中的相对路径),也可以是文件夹,如果是文件夹还可以指定是否创建文件夹

实例方法和属性:

  • public string GetFileFullPath(string fileWithProjectName):根据给定的文件名称,获取文件的全路径,文件名称的格式可以是以下3种之一:filename@projectname、projectname:filename、filename(模板文件所在的项目)
  • public DTE DTE{get;set;}:DTE
  • public Project CurrentProject{get;set;}:tt模板文件所在的项目
  • public ProjectItem TemplateItem{get;set;}:tt模板文件
  • public string AssemblyName{get;set;}:当前项目的程序集名称
  • public string RootNamespace{get;set;}:当前项目的默认命名空间

构造函数:

  • public VSHost(GeneratedTextTransformation trans)

目前在我们项目中这些方法和属性就能满足要求了,需要的话可以再添加 微笑

EDMX文件合并器:EdmxMerger.ttinclude

顾名思义,这个是合并多个edmx文件的。

我们项目采用的是EntityFramework,在edmx的设计器中,类太多了的话使用起来不是很方便,但是VS自己不带edmx合并功能,那就自己做一个吧 微笑

使用也很简单:

<#@ template  debug="true" hostSpecific="true" #>
<#@ include file="..\EdmxMerger.ttinclude" #><#@
output extension=".edmx"#><#
//If you want to debug, please remove this comment
//System.Diagnostics.Debugger.Launch();
string[] edmxFiles = new String[]{@"Path\filename1.edmx@ProjectName1",
    @"ProjectName2:Path\filename2.edmx",
    @"filenameInCurrentProject.edmx"};
string storeNamespace = "YourStoreNamespace";
string containerName = "YourContainerName";
string Namespace= "YourClassNamespace";
Merge(edmxFiles, containerName, Namespace,storeNamespace);
#>

只需要指定edmxFiles这个数组就可以了,注意先后次序。

目前不支持复杂类型和存储过程以及视图(我们项目中都没有使用到)

T4模板的脚手架:MvcViewTransfer.ttinclude

关于asp.net mvc的脚手架,请参考TNT2(SZW)的:http://www.cnblogs.com/szw/archive/2010/05/18/1738120.html

这个Include也是参考TNT2(SZW)的系列文件写出来的。

同样使用也很简单:

<#@ template  debug="true" hostSpecific="true"#>
<#@ include file="..\MvcViewTransfer.ttinclude" #><#@
output extension=".txt"#>
<#
//If you want to debug, please remove this comment
//System.Diagnostics.Debugger.Launch();
string destProjectName = "YourMVCViewProject";
string[] sourceFolders = new string[]{"Views", "Areas\\Admin\\Views","Areas\\Photo\\Views"};
//If you want to transfer to different folder names 
//string[] destFolders = new string[]{"Views1", "Areas\\Admin\\Views1",""};
//Transfer(destProjectName, sourceFolders,destFolders);
Transfer(destProjectName, sourceFolders);
#>

先简单介绍到这里。

关于ttinclude文件的位置,可以参考我的这篇文章:VS2008 T4模板include文件的查找位置

要使用这些include,需要安装Visual Studio 2010 SDK,在vs2008下没有测试过:

http://www.microsoft.com/downloads/en/confirmation.aspx?familyid=47305CF4-2BEA-43C0-91CD-1B853602DCC5&displaylang=en

参考了以下文章:

原文地址:https://www.cnblogs.com/tubo/p/1873390.html