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";
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 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.
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?