Part III: Examples for the Using of the Code Document Object Model

Design Time Support – Serializers

When client side components are developed, especially those in direct association with the user interface, they require support of design-time capabilities. These capabilities often include the automatic insertion of code portions into a source file.
For instance, should you drag a Button unto a Form and double click it in the Visual Studio.NET designer, an event registration would be added to the form’s InitializeComponent method and a new empty method would be created.
Another example is dragging a DataAdapter unto a form, which then lets you define tables and columns in a visually appealing way and then translates your actions into lines of code inside the form.
These lines of code are usually persisted from an object model graph that was manipulated by the component’s developer. The developer wrote a method which received the form’s code document’s object graph before the component’s designer came into play, manipulated it according to the component’s needs and returned it for any other components to manipulate it as well.

The Recurring Case of the Typed Collection

Dynamic collections are common when working with .NET. The collections supplied with the Framework are usually not for certain types, but for the general type of object, such as ArrayList.
The problems begin when we have to create Typed Collections – dynamic collections that would accept only variables of a certain type. To create such a collection, a new class must be written, which would implement several methods, such as Add, Remove, etc. Think of creating such collections many times over, one for every type you need. Depressing, isn’t it?
Today, the C# language does not support generics. This means that we have to code everything manually, creating methods such as:

public void Add (T value)
{
base.InnerList.Add(value);
}

This method would limit the collection to only receiving variables of type T.

One solution that we can create for this problem using Code DOM is creating the entire implementation, leaving the type dynamic. We could create a method that from this call:

CollectionGenerator.Generate(typeof(int));

Such methods as the following would be generated:

public void Add (int value)
{
base.InnerList.Add(value);
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s