views:

997

answers:

1

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.

+6  A: 
  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.

Oleg Sych
Hi Oleg, thank you for your answer :-)I read your blogs related to the T4 engine all the time.<br /><br />I am currently trying by looking at the Source Code of the ** MvcTextTemplateHost** class using the .NET Reflector.<br />Since I don't have the VS2010 on my computer yet, I guess I will take a look at it later for the EF 2.0 T4 templates.<br /><br />My 2nd attempt will be to look at the VS Code Model URL you posted.Thank you so much :-)
stun