Tuesday, May 15, 2007

Design of ObjectBuilder I


ObjectBuilder Architecture

ObjectBuilder uses a pipeline of strategies. With this pipeline, multiple operations can take place as objects are instantiated and prepared for use. This means you can control the order in which the processes take place. It also supports controlled disposal of object instances by executing the appropriate pipeline processes in the reverse order.
ObjectBuilder strategies manage the processes performed on objects during construction and disposal.

ObjectBuilder Methods
ObjectBuilder resides in the namespace Microsoft.Practices.ObjectBuilder, and the base class BuilderBase exposes two methods. The

1. BuildUp method has two overloads.

.BuildUp(locator, type, id, instance, policies[] );
.BuildUp(locator, id, instance, policies[] );


locator - This is a reference to a class that implements the IReadWriteLocator interface; it provides hints as to the location of the object or class.
type - This is the type of object to create.
id - This is the identifier to assign to the new object instance.
instance - This is an optional reference to an existing object instance on which the pipeline process will act. This allows you to take existing objects and make sure that they are properly prepared for use by passing them through the pipeline.
policies[] - This is an array of PolicyList instances that implement transient policies that override the built-in policies.


2. The TearDown method takes an existing object instance and runs it back through the strategy chain. This process can remove features added to the object during the BuildUp process if you want to re-use the object.

public TItem TearDown(IReadWriteLocator locator, TItem item)


Using a ReadWriteLocator
ObjectBuilder uses the concept of a lifetime container to store object references, which ensures the correct management of objects and their disposal at the correct times. Locators reference individual objects within the lifetime container. Multiple items in a locator can point to the same object instance. For example, items in a locator can reference it through the class type name, the interfaces it implements, and the object name.

Using a PolicyList
A PolicyList is a set of policies, each represented by classes that implement the IBuilderPolicy interface, that together influence the way that ObjectBuilder creates an object. Policies can be transient or permanent.

Transient policies are those automatically generated by the strategies in ObjectBuilder or through reflection over the attributes on the methods and properties declared in the class file.

Permanent policies are those you create by implementing the IBuilderPolicy interface. You can pass an array of these class instances to the BuildUp method in ObjectBuilder.

No comments: