ASP.NET Dynamic Data Tutorial - BuddyMetadataProvider and Custom Metadata Providers

ASP.NET Dynamic Data Tutorial - BuddyMetadataProvider and Custom Metadata Providers

by David Hayden, Florida ASP.NET Developer, Filed: ASP.NET 3.5

 

This is my 5th tutorial on the new ASP.NET Dynamic Data that ships with the ASP.NET 3.5 Extensions CTP. ASP.NET Dynamic Data is an alternative to code generation in helping one build database-driven web applications in a more productive manner.

If you are interested, you can read the past four tutorials:

 

The Importance of Metadata to ASP.NET Dynamic Data

It seems pretty obvious now that the success of ASP.NET Dynamic Data is dependant on the ease and flexibility of the metadata you can attach to your data access model to influence the UI and cross-cutting concerns like logging, exception handling, validation, security, etc. If we end up jumping through hoops to get the Dynamic Data Framework to do things it doesn't do out of the box, this framework won't make it out of the driveway. Sure you will have those who hack at it to build a database-driven website at the expense of maintainability, but we're not like that.

 

So Far We've Been Sticking the Metadata on Partial Classes

If you read the previous ASP.NET Dynamic Data Tutorials, we have been sticking our metadata on partial LINQ To SQL Classes. The metadata has been in the form of attributes on the class itself. Here are a couple examples mentioned in the tutorials:

 

[Range("UnitsInStock",10,50,
    ErrorMessage="Must be between {1} and {2}")]
public partial class Product
{
}

 

[RenderHint("HireDate","CalendarDateTime")]
public partial class Employee
{
}

 

The first example uses the RangeAttribute for validation of the UnitsInStock Property on the Product Class. The second examples uses the RenderHintAttribute to tell the Dynamic Data Framework that the HireDate Property needs to be displayed using the CalendarDataTime Dynamic Data Field.

I use attributes quite a bit in my .NET Applications, so I am perfectly fine with their use on partial classes. Developers that take advantage of code generation will typically have a partial class that consists of a code generated version that is to remain untouched and a hand-created version with custom methods, properties, rules, etc.

 

Custom Metadata Providers in ASP.NET Dynamic Data

If you don't like adding metadata to partial classes via attributes, the MVCToolkit contains a couple of custom metadata providers that you can use as a vehicle for adding metadata to your Dynamic Data Applications in different ways:

  • BuddyMetadataProvider
  • XmlMetadataProvider

I will show off the BuddyMetadataProvider here.

 

BuddyMetadataProvider

The BuddyMetadataProvider is a custom metadata provider in the MVCToolkit that allows you to add metadata to a buddy class that is associated with the real class. That is clear as mud, so here is an example using the UnitsInStock Validation we mentioned above.

Here is one way that I have been validating that the UnitsInStock entered for a Product is between 10 and 50 using the RangeAttribute. The metadata is on the partial class itself:

 

[Range("UnitsInStock",10,50,
    ErrorMessage="Must be between {1} and {2}")]
public partial class Product
{
}

 

As an alternative to adding the attribute on the Product Partial Class, I could register the BuddyMetadataProvider in Application_Start:

 

protected void Application_Start(object sender, EventArgs e) {
 MetadataHandlerRegistration.Register(
typeof(DataClassesDataContext), MetadataHandlerRegistration.BuddyMetadataProvider); }

 

and introduce a buddy class that holds the metadata for the Product Class:

 

public class Product_Metadata
{
    [Range(10, 50,
        ErrorMessage = "Must be between {1} and {2}")]
    public System.Nullable<short> UnitsInStock { get; set; }
}

 

Unless you specify otherwise, ASP.NET Dynamic Data looks for a buddy class that ends in “_Metadata“. Therefore, our buddy class is named Product_Metadata.

The validation result is the same on the UI:

 

ASP.NET Dynamic Data Validation

 

 

Optionally, You can explicity specify the buddy class via the MetadataClassAttribute:

 

[MetadataClass(typeof(Product_Metadata))]
public partial class Product
{
}

 

Conclusion

The goal with Metadata Providers in ASP.NET Dynamic Data is to offer a bit of flexibililty as to where metadata is provided. In this case we introduced the metadata as attributes on a buddy class with the help of the BuddyMetadataProvider. There is also an XmlMetadataProvider in the MVCToolkit as well as examples of its use.

You will need the MVCToolkit to try this out.

 

Site: http://www.davidhayden.com/

Author: David Hayden, Florida ASP.NET Developer

Tag: ASP.NET 3.5

posted on Sunday, January 06, 2008 1:20 PM

My Links

Post Categories

Article Categories

Archives

Loose-Leaf Tea