A basic T4 template for generating Model Metadata in ASP.NET MVC2 - Raj Kaimal

A basic T4 template for generating Model Metadata in ASP.NET MVC2

I have been learning about T4 templates recently by looking at the awesome ADO.NET POCO entity generator. By using the POCO entity generator template as a base, I created a T4 template which generates metadata classes for a given Entity Data Model. This speeds coding by reducing the amount of typing required when creating view specific model and its metadata.

To use this template,

1) Download the template provided at the bottom.

2) Set two values in the template file. The first one should point to the EDM you wish to generate metadata for. The second is used to suffix the namespace and classes that get generated.

string inputFile = @"Northwind.edmx";
string suffix = "AutoMetadata";

3) Add the template to your MVC 2 Visual Studio 2010 project.

Once you add it, a number of classes will get added to your project based on the number of entities you have. 

image 
One of these classes is shown below. Note that the DisplayName, Required and StringLength attributes have been added by the t4 template.
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
 
using System; 
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
 
namespace NorthwindSales.ModelsAutoMetadata
{
    public partial class CustomerAutoMetadata
    {
            
        [DisplayName("Customer ID")]
        [Required]
        [StringLength(5)]
        public string CustomerID { get; set; }
            
        [DisplayName("Company Name")]
        [Required]
        [StringLength(40)]
        public string CompanyName { get; set; }
            
        [DisplayName("Contact Name")]
        [StringLength(30)]
        public string ContactName { get; set; }
            
        [DisplayName("Contact Title")]
        [StringLength(30)]
        public string ContactTitle { get; set; }
            
        [DisplayName("Address")]
        [StringLength(60)]
        public string Address { get; set; }
            
        [DisplayName("City")]
        [StringLength(15)]
        public string City { get; set; }
            
        [DisplayName("Region")]
        [StringLength(15)]
        public string Region { get; set; }
            
        [DisplayName("Postal Code")]
        [StringLength(10)]
        public string PostalCode { get; set; }
            
        [DisplayName("Country")]
        [StringLength(15)]
        public string Country { get; set; }
        [DisplayName("Phone")]
        [StringLength(24)]
        public string Phone { get; set; }
            
        [DisplayName("Fax")]
        [StringLength(24)]
        public string Fax { get; set; }
    }
}

 

The following shows how the generated class can be used from your project by creating a partial class with the entity name and setting the MetadataType attribute. Note that any changes to the auto generated files will be overwritten. You can also copy the code in the metadata class generated and create your own ViewModel class. In this case, you are responsible for keeping things in sync just like you would with any ViewModel class.

 
namespace MyProject.Models
{
   [MetadataType(typeof(CustomerAutoMetadata))]
    public partial class Customer
    {
      
    }
}

 
The template is super basic  and does not take into account complex properties. I have tested it with the Northwind database.  Feel free to modify the template to suite your requirements.

Standard disclaimer follows:
Use At Your Own Risk, Works on my machine running VS 2010 RTM/ASP.NET MVC 2

Mr. Incredible: Of course I have a secret identity. I don't know a single superhero who doesn't. Who wants the pressure of being super all the time?

Published Tuesday, May 4, 2010 1:40 AM by rajbk
Filed under: , , ,

Comments

# re: A basic T4 template for generating Model Metadata in ASP.NET MVC2

Thanks, I've been working on a similar template and you've saved me some time.

Thursday, May 6, 2010 7:43 AM by Jeff Circeo

# re: A basic T4 template for generating Model Metadata in ASP.NET MVC2

Very nice. Saves a lot work

Thursday, September 23, 2010 3:57 AM by Jasper

# re: A basic T4 template for generating Model Metadata in ASP.NET MVC2

You may get a null ref when running this template on tables who have varchar(max) types.  In the WriteStringLengthAttribute method, just check the maxLengthfacet.Value isn't null before continuing.

E.g.

if (edmProperty.TypeUsage.Facets.TryGetValue("MaxLength", true, out maxLengthfacet))

{

double lengthAttribute;

if (maxLengthfacet.Value == null) { return; }

Sunday, October 10, 2010 11:09 PM by kato