A basic T4 template for generating Model Metadata in ASP.NET MVC2
- by rajbk
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, Download the template provided at the bottom. 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";
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.
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 gen’d class can be used from your project by creating a partial class with the entity name and setting the MetadataType attribute.namespace MyProject.Models{ [MetadataType(typeof(CustomerAutoMetadata))] public partial class Customer { }}
You can also copy the code in the metadata class generated and create your own ViewModel class.
Note that the template is super basic and does not take into account complex properties. I have tested it with the Northwind database. This is a work in progress. 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
AutoMetaData.zip
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?