Using Data Annotations and Extensions with SubSonic 3.0

I was just about to search for a good Url regular expression to check input validation, when I remembered a recent blog post by @srkirkland mentioning Data Annotation Extensions (github).  This extension package allows for additional common use data annotations, such as URL, Email, Credit Card, etc in any .NET 4.0 project. It has been released as a NuGet package so you can easily implement it in your solution in just a click.

The catch was that I'm using SubSonic 3.0.  I love SubSonic, but I couldn't think of an easy way to implement Data Annotations in conjunction with it.   So... I searched Stackoverflow like usual, and found a couple good posts.  The short of it is to create an additional TT file that creates buddy classes.

Here is the walkthrough:

Note that the MetaGenerator TT file referred to in the above post must be created manually by you and is not included with SubSonic by default.  Actually you could call it whatever you like.  The code is included in the posted link above.

In my MetaGenerator.TT (download) I added an extra line to the includes:

using DataAnnotationsExtensions;

Once saved, right click the file and select "Run Custom Tool" to generate the .cs (MetaGenerator.cs) file.  Open that up and start adding your Data Annotations, and if you've so chosen, the Data Annotations Extensions as well.

I guess the only "gotcha" here is that if change your DB schema and you re-run the custom tool (TT file), it will wipe any custom input you've created.  Perhaps there is a work around for that?