且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-12-02 21:22:10


  1. 您可以使用Reflector从C:\\反编译MvcTextTemplateHost的源代码\\程序文件(x86)\Microsoft Visual Studio 9.0\Common7\IDE\Microsoft.VisualStudio.Web.Extensions.dll程序集。是的,您可以使用

  2. /envdte.codemodel.aspxrel =nofollow noreferrer> CodeModel 。但是,直接从edmx文件读取它可能更好。无论哪种方式,这是一项重大任务。在VS 2010中,可能会有一个如何以EF T4模板形式进行此操作的示例。


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.

For Reference

Please take a brief look at the following two URLs.

  1. Scott Hanselman - "T4 Code Generation Visual Studio Best Kept Secret"
  2. Visual Web Developer Team Blog - Quick Start Guide for ASP.NET MVC Developers

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).

What I Have Right Now

  1. MVC T4 Template files (e.g., Controller.tt, Create.tt, etc...) have been included as part of my MVC Web Project.
  2. I have an ADO.NET Entity Framework MyModel.edmx file in the "Models" folder.

Based on the Controller name (e.g. "ProductController"), I want to retrieve the [System.Type] information of the "Product" class from the ADO.NET Entity Framework model. I want to be able to retrieve System.Type information the same way as the MVC View T4 files (e.g. Edit.tt) as below.

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
and
generate basic CRUD operations and method signatures for EDIT, DETAILS, ADD operations etc...

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.

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.

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.

Some Things that Will Help Me Find the Solution

  1. Where do I find the Source Code for the MvcTextTemplateHost class? 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.
  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?

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.

  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.

  2. Yes, you can load type metadata from a Visual Studio using CodeModel. However, you may be better off reading it from the edmx file directly. 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.