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

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

问题描述

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

仅供参考

请在采取以下两个URL简单的介绍一下。


  1. 斯科特Hanselman的 - <一href=\"http://www.hanselman.com/blog/T4TextTemplateTransformationToolkit$c$cGenerationBestKeptVisualStudioSecret.aspx\"相对=nofollow> T4 code代Visual Studio中最不为人知

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

简要说明什么,我想实现

使用T4引擎来生成基于ADO.NET实体框架模型的主键(S)与行动方法MVC Controller类。

我现在做什么右


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

  2. 我曾经在模式文件夹中的ADO.NET实体框架的 MyModel.edmx文件

基于控制器的名字(例如ProductController的)

,我想找回在 [System.Type的] 产品<信息/ STRONG>从ADO.NET实体框架模型类。
我希望能够找回的System.Type 信息同样的方式MVC视图T4文件的(例如Edit.tt)如下图所示。

  MvcTextTemplateHost mvcHost =(MvcTextTemplateHost)(主机);
类型type = mvcHost.ViewDataType;

最终目标

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


生成编辑,详情基本CRUD操作和方法签名,添加操作等等...

我在哪里卡住

然而,正如你可以从快速入门指南ASP.NET MVC开发者文章中,我不能检索的 [System.Type的] 对于控制器 T4模板,因为在 MvcTextTemplateHost 类仅公开的 ViewDataType 属性用于创建MVC视图。

我试图检索[System.Type的]由以下技术不工作,因为在 modelType 被返回的无效这意味着它无法找到的类型

 键入modelType = Type.GetType(modelFullyQualifiedName,假的,真正的);

我假定这是因为实体框架模型是作为我的MVC Web项目的的一部分和不包括作为编译的.dll库程序集的一部分。

这将有助于我找到解决办法

有些事情

  1. 我在哪里可以找到源头code为 MvcTextTemplateHost 类?如果我能找到至少DLL文件,我可以大概看一下code如何加载在Visual Studio中输入的类型信息的添加视图对话框窗口

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

我真的AP preciate如果有人能帮助我在此话题,因为这将让我产生code为MVC控制器操作方法ADD的75%,编辑,细节,等..和基本的CRUD操作code。


解决方案

  1. 您可以使用反射来从C编译MvcTextTemplateHost来源$ C ​​$ C:\\ Program Files文件(x86)的\\微软的Visual Studio 9.0 \\ Common7 \\ IDE \\ Microsoft.VisualStudio.Web.Extensions.dll组装。


  2. 是的,你可以使用的 codeModel 。但是,你可能会更好从EDMX文件直接读取它。无论哪种方式,这是一个相当大的任务。有可能是如何做到这一点,在2010 VS 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和定制&QUOT;控制器&QUOT; T4模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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