T4 Metadata and Data Annotations Template

Awhile back I was working on an ASP.NET MVC 3 project that required a lot of “standard” validation to be performed (required fields, validate lengths, some regular expression validation here and there, data type checks, etc.). Due to the number of classes involved I decided to go with the data annotations approach to keep things nice and tidy (some people love this, some people hate it – such is life :-)). As I dug more into the project I realized it was a fairly monumental task to create a metadata class for each of my model classes so I ended up investing one night to create a Visual Studio T4 template that could generate all of the metadata classes automatically based on an Entity Framework 4 model. I published the project up on CodePlex to make it easy for other devs to get to but realized I never blogged about it. Here’s an overview of what the T4 metadata and data annotations template does.

The T4 template handles generating metadata classes from an Entity Framework 4 model and decorates properties with data annotation attributes such as [Required] and [StringLength]. The [DataType] attribute is also applied when appropriate. It'll also generate ErrorMessage values based upon property names for required fields. Camel-cased property names will automatically be split up so that the description doesn't have to be modified in many cases. Once the template generates the metadata classes you can then use them in your project and make modifications as needed (keep in mind you'll probably want to remove the template once it generates the initial code since any further executions will overwrite any changes you may make). An example of a class and associated metadata class generated by the template is shown next:

[MetadataType(typeof(EmploymentMetadata))]
public partial class Employment
{
    internal sealed class EmploymentMetadata
    {
		
	[Required(ErrorMessage="Id is required")]
    	public Int32 Id { get; set; }

	[Required(ErrorMessage="Company Name is required")]
	[StringLength(250)]
    	public String CompanyName { get; set; }

	[Required(ErrorMessage="Supervisor is required")]
	[StringLength(250)]
    	public String Supervisor { get; set; }

	[StringLength(100)]
	[DataType(DataType.EmailAddress)]
    	public String EmailAddress { get; set; }

	[Required(ErrorMessage="Phone Number is required")]
	[StringLength(15)]
	[DataType(DataType.PhoneNumber)]
    	public String PhoneNumber { get; set; }

	[DataType(DataType.DateTime)]
    	public DateTime DateStart { get; set; }

	[DataType(DataType.DateTime)]
    	public DateTime DateEnd { get; set; }

	[Required(ErrorMessage="Reason For Leaving is required")]
	[StringLength(1000)]
    	public String ReasonForLeaving { get; set; }

    }
}


To use this T4 template in Visual Studio 2010 follow the steps below:

  1. Download the sample project from the Downloads area of the project to see the template in action
  2. Add the template .tt file into your Entity Framework 4 project
  3. Change the Source CsdlPath to point to your Entity Framework 4 .edmx file within the T4 template
  4. The name you give the .tt file will be the name given to the code file it generates
  5. Don't make changes directly to the generated code as you customize your data annotations. You'll need to copy it to a file you control. Otherwise the template may overwrite changes you make.

This is one of those "works on my machine" type of projects. I needed to simplify the process of creating metadata buddy classes and decided to bite the bullet and create something custom since I couldn't find anything out there that did this (although I'm guessing there’s probably something out there somewhere). Feel free to contribute to the project on CodePlex if you make any enhancements.

comments powered by Disqus

2 Comments

Comments have been disabled for this content.