简体   繁体   English

MVC T4 MvcTextTemplateHost和自定义“控制器”T4模板

[英]MVC T4 MvcTextTemplateHost and Customized “Controller” T4 Template

I am creating my own custom T4 Template that integrates with an ADO.NET Entity Framework Model (.edmx file) included in my MVC Web Application. 我正在创建自己的自定义T4模板,该模板与我的MVC Web应用程序中包含的ADO.NET实体框架模型(.edmx文件)集成。

For Reference 以供参考

Please take a brief look at the following two URLs. 请简要了解以下两个网址。

  1. Scott Hanselman - " T4 Code Generation Visual Studio Best Kept Secret " Scott Hanselman - “ T4 Code Generation Visual Studio Best Kept Secret
  2. Visual Web Developer Team Blog - Quick Start Guide for ASP.NET MVC Developers Visual Web Developer团队博客 - ASP.NET MVC开发人员快速入门指南

Short Description of What I am trying to Achieve 简要说明我想要实现的目标

Use the T4 engine to generate the MVC Controller class with Action methods based on the ADO.NET Entity Framework Model's Primary Key(s). 使用T4引擎使用基于ADO.NET实体框架模型的主键的Action方法生成MVC Controller类。

What I Have Right Now 我现在拥有什么

  1. MVC T4 Template files (eg, Controller.tt, Create.tt, etc...) have been included as part of my MVC Web Project. MVC T4模板文件(例如,Controller.tt,Create.tt等...)已作为我的MVC Web项目的一部分包含在内。
  2. I have an ADO.NET Entity Framework MyModel.edmx file in the " Models " folder. 我在“ Models ”文件夹中有一个ADO.NET Entity Framework MyModel.edmx文件

Based on the Controller name (eg "ProductController") , I want to retrieve the [System.Type] information of the "Product" class from the ADO.NET Entity Framework model. 基于Controller名称(例如“ProductController”) ,我想从ADO.NET Entity Framework模型中检索“Product”类的[System.Type]信息。 I want to be able to retrieve System.Type information the same way as the MVC View T4 files (eg Edit.tt) as below. 我希望能够以与MVC View T4文件(例如Edit.tt)相同的方式检索System.Type信息,如下所示。

MvcTextTemplateHost mvcHost = (MvcTextTemplateHost) (Host);
Type type = mvcHost.ViewDataType;

Final Goal 最终目标

I want to create the Controller method code-generation to read Primary Key information and etc from the ADO.NET Entity Framework Class via Reflection 我想创建Controller方法代码生成,以通过Reflection从ADO.NET实体框架类中读取主键信息等
and
generate basic CRUD operations and method signatures for EDIT, DETAILS, ADD operations etc... 为EDIT,DETAILS,ADD操作等生成基本的CRUD操作和方法签名...

Where I am Stuck 我被困在哪里

However, as you can see from Quick Start Guide for ASP.NET MVC Developers article, I cannot retrieve [System.Type] for Controller T4 Template because the MvcTextTemplateHost class exposes only ViewDataType property for creating MVC Views. 但是,正如您可以从ASP.NET MVC开发人员快速入门指南中看到的那样,我无法检索Controller T4模板的[System.Type] ,因为MvcTextTemplateHost类仅公开用于创建MVC视图的ViewDataType属性。

My attempt to retrieve the [System.Type] by the following technique is NOT working because the modelType is being returned as null meaning it can't find the type. 我尝试通过以下技术检索[System.Type]是不行的,因为modelType被返回为null意味着它找不到类型。

Type modelType = Type.GetType(modelFullyQualifiedName, false, true);

I am assuming that this happens because the Entity Framework Model is included as part of my MVC Web Project and not included as part of a compiled .DLL library assembly. 我假设这是因为实体框架模型作为我的MVC Web项目的一部分包含在内, 不是作为已编译的.DLL库程序集的一部分包含在内。

Some Things that Will Help Me Find the Solution 一些可以帮助我找到解决方案的事情

  1. Where do I find the Source Code for the MvcTextTemplateHost class? 我在哪里可以找到MvcTextTemplateHost类的源代码? If I can find at least the DLL file, I can probably look at how the code loads the Type information entered in the Visual Studio "Add View" dialog window . 如果我至少可以找到DLL文件,我可以查看代码如何加载在Visual Studio “添加视图”对话框窗口中输入的类型信息。
  2. Is there a way to dynamically retrieve System.Type information of a Class included in the Visual Studio project from my T4 Template through the Visual Studio IDE API? 有没有办法通过Visual Studio IDE API从我的T4模板动态检索Visual Studio项目中包含的类的System.Type信息?

I would really appreciate if anyone can help me on this topic as this will allow me to generate 75% of the code for MVC Controller Action Methods for ADD,EDIT,DETAILS, etc.. and basic CRUD operation code. 我真的很感激,如果有人可以帮助我这个主题,因为这将允许我为ADD,EDIT,DETAILS等MVC控制器操作方法生成75%的代码和基本的CRUD操作代码。

  1. You can use Reflector to decompile source code of MvcTextTemplateHost from C:\\Program Files (x86)\\Microsoft Visual Studio 9.0\\Common7\\IDE\\Microsoft.VisualStudio.Web.Extensions.dll assembly. 您可以使用Reflector从C:\\ Program Files(x86)\\ Microsoft Visual Studio 9.0 \\ Common7 \\ IDE \\ Microsoft.VisualStudio.Web.Extensions.dll程序集中反编译MvcTextTemplateHost的源代码。

  2. Yes, you can load type metadata from a Visual Studio using CodeModel . 是的,您可以使用CodeModel从Visual Studio加载类型元数据。 However, you may be better off reading it from the edmx file directly. 但是,您最好直接从edmx文件中读取它。 Either way, this is a substantial task. 无论哪种方式,这都是一项重大任务。 There may be a usable example of how to do this in the form of EF T4 template in VS 2010. 在VS 2010中,可能有一个如何以EF T4模板的形式执行此操作的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM