Posts tagged ‘mvc’

September 17, 2010

Code driven auto-model generation using DynamicObject

The DynamicObject base class is a new base class that was released with version 4 of the .NET framework. The DynamicObject base class gives us a number of very cool pieces of functionality, particularly in ORM applications, which will be very familiar to users of interpreted languages such as PHP, Python, Ruby, etc.

DynamicObject sits in the System.Dynamic name space, and gives us inverse functionality of the dynamic keyword. Where the dynamic keyword, when applied to a variable allows method resolving at runtime, such as

dynamic myObj = new MyObj();

//MyObj may or may not have a method called run, this will be resolved at runtime;

DynamicObject allows an object to resolve, at runtime, calls to it’s own methods. Instances of DynamicObject will preferably be instanciated as dynamic objects, as above, but in addition to the call being resolved at runtime, the object itself will respond to the call at run time.

To this end DynamicObject gives us a number of basic methods to override, which will allow us to access and set Properties (TryGetMember, TrySetMember) and Methods (TryInvokeMember). In addition, it allows us to specify what happens when the object is interacted with as an array (TryGetIndex, TrySetIndex, TryDeleteIndex).

As an example, the TryGetMember may be overridden as follows

FieldCollection myCustomFields = new FieldCollection();
PropertyCollection properties = new PropertyCollection();

public override bool TryGetMember(GetMemberBinder binder, out object result)
	//Check if we hold a field of this type
	if (myCustomFields.ContainsField(binder.Name))
		//return the property object associated with that name
		result = properties[binder.Name];
	return base.TryGetMember(binder, out result);

As can be seen this code is implemented very simply, and, in this case, we can use a simple dictionary structure to store names against values.

Where this can really come into its own is in auto generating models based in database definitions. Using the metadata views in SQL Server (all_objects, all_columns etc.), we can pull the fields for a table, and, in the constructor of the dynamic object populate our fields collection. This means when we do CRUD operations, such as a load, we can build queries dynamically, based on the composition of the model. The best thing about this is that no objects need to be changed when database structure is changed, new properties are simply accessed as a property of the dynamic object.

As such we can build up the Fields by pulling all User generated fields for a particular table from all_columns view in a SQL database, filtering by the object name, joining to all_objects view, and the object type (‘U’ for user).

Using this pattern we can build a base class which needs very little extension to handle the vast majority of objects, and we only extend when we need to implement validation, and individual business logic.