MVC T4 MvcTextTemplateHost和定制的“控制器” T4模板 [英] MVC T4 MvcTextTemplateHost and Customized "Controller" T4 Template

查看:159
本文介绍了MVC T4 MvcTextTemplateHost和定制的“控制器” T4模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建自己的定制T4模板,该模板与我的MVC Web应用程序中包含的ADO.NET实体框架模型(.edmx文件)集成。





请简要介绍以下两个网址:


  1. Scott Hanselman - T4代码生成Visual Studio最好的秘密

  2. Visual Web Developer团队博客 - ASP.NET MVC开发人员快速入门指南



简短描述我正在尝试实现



使用T4引擎使用基于ADO.NET实体框架模型的主键的Action方法生成MVC控制器类。



我现在有了




  1. MVC T4模板文件(例如Controller.tt, Create.tt,etc ...)已被包含作为我的MVC Web项目的一部分。

  2. 我在模型中有一个ADO.NET实体框架 MyModel.edmx文件文件夹。

根据控制器名称(例如ProductController),我想从ADO.NET实体框架模型中检索Product类的 [System.Type] 信息。
我希望能够以与以下MVC View T4文件(例如Edit.tt)相同的方式检索 System.Type 信息。

  MvcTextTemplateHost mvcHost =(MvcTextTemplateHost)(Host); 
键入type = mvcHost.ViewDataType;



最终目标



我要创建控制器方法代码生成从ADO.NET实体框架类通过反射读取主键信息等



生成基本CRUD操作和方法签名用于编辑,细节,添加操作等...



我在哪里住



,您可以从 ASP.NET MVC开发人员快速入门指南文章中看到,我无法检索控制器 T4模板的 [System.Type] 因为 MvcTextTemplateHost 类仅显示用于创建MVC视图的 ViewDataType 属性。



我尝试检索[System。类型]由于 modelType 被返回为 null ,因此无法找到该类型。

 键入modelType = Type.Get Type(modelFullyQualifiedName,false,true); 

我假设这是因为实体框架模型作为我的MVC Web Project 不作为编译的.DLL库程序集的一部分。



有些事情会帮助我找到解决方案




  1. 我在哪里可以找到 MvcTextTemplateHost 类的源代码?如果我可以找到至少DLL文件,我可以看看代码如何加载在Visual Studio 添加视图对话框窗口中输入的类型信息。

  2. 有没有办法通过Visual Studio IDE API从T4模板动态检索Visual Studio项目中包含的类的System.Type信息?

我真的很感激任何人可以帮助我这个话题,因为这将允许我生成75%的MVC控制器操作方法的ADD,EDIT,DETAILS等代码和基本的CRUD操作代码

解决方案


  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.

这篇关于MVC T4 MvcTextTemplateHost和定制的“控制器” T4模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆