Friday, January 25, 2008

Dynamically Extending Objects in C# - Part 1

So you've finished your domain model, it is looking beautiful and performing great, but the business throws a curve ball. They need to add a couple of new fields critical to their needs, and it needs to happen yesterday.

Sure, this is not the hardest thing to do in most applications. Whip out, the mapping files, add the field to the xml, db & object and you are done.

However, this is not always the case. For example, I've recently been working in a system that does a lot of cross schema data manipulation, through batch processes and triggers. What this means is that adding a field is no easy affair. I add the field in a upstream schema/object model and then I modify the routines that move it downstream to the next schema/object model and downstream, and on, and on.

Well the idea here mostly applies to Windows Forms but it could also apply to Web Forms. By making use of the System.ComponentModel namespace, we can extend our business object's meta data at run-time and change the behavior of our databound controls.

In this manner I can add new properties to any existing type at runtime and get these to be automatically picked up by the UI controls bound to that type. Consequently, I can have the plumbing laid out in expectancy of DEFCON 1 type scenarios.

So how does it work?

If you've poked around the System.ComponentModel namespace you've probably stumbled upon this beautiful diamond named TypeDescriptor. It has methods like GetProperties(), GetEvents(), etc. TypeDescriptor basically wraps some of the System.Reflection basic constructs and adds various extensibility points which are used by controls when databinding.

For our purpose I'll concentrate in the GetProperties() method which returns a PropertyDescriptorCollection. A PropertyDescriptor exposes all the basic metadata information about a property like, PropertyType, Name, etc, along with other information like DisplayName and Description. It also has some methods to Set and Get the value of the property for an instance.

So how can we leverage this property descriptor to do our bidding? Let's say for example you have a grid bound to your object. In this case let's say it is an Infragistics UltraGrid (since I know this works in this grid). Let's take a simple scenario and say that I want to change the column names in my grid dynamically. The grid obtains the databound object column names by making a call to TypeDescriptor.GetProperties() and checks the DisplayName for each of the object's properties. Well what I want to do is obtain the name of the columns from somewhere other than the attributes on the object.

public interface IPropertyMetaDataService
{

string GetDisplayName(Type componentType, string propertyName);
...

}

Implementing this interface allows me to get a display name for a type/property name combo.

public sealed class MetaPropertyDescriptor : PropertyDescriptor
{

private PropertyDescriptor innerPropertyDescriptor;
private IPropertyMetaDataService PropService { get { return ... ;} }

public override string DisplayName
{

get
{

string displayName = null;

displayName = PropService.GetDisplayName(

innerPropertyDescriptor.ComponentType,
innerPropertyDescriptor.Name);

return string.IsNullOrEmpty(displayName) ?

innerPropertyDescriptor.DisplayName :
displayName;

}
}
...

}

And, by inheriting from Property Descriptor I can override the behavior of my databound components. Voila, now with a little more boilerplate code I can get my grid to display column names from my PropertyMetaDataService.

In the next part I'll discuss a couple more interesting things that can be done using this technique.

Thursday, January 24, 2008

The road less travelled

A couple of guys have been bugging me @ work to start a blog.

A blog technical in nature, though not guaranteed to be, about many different things that interest me as a developer. I hope this blog will keep company to people that are traveling the same road I am.

So without much further ado, here it is, the first post and first step of many to come (hopefully).