Showing posts with label ObjectBuilder. Show all posts
Showing posts with label ObjectBuilder. Show all posts

Tuesday, May 15, 2007

Design of ObjectBuilder II

Pipeline Stages
The pipeline in ObjectBuilder consists of the following four stages:
  • PreCreation - This stage occurs before ObjectBuilder creates a new instance of the object.
  • Creation - In this stage, ObjectBuilder creates the new object instance.
  • Initialization - In this stage, ObjectBuilder prepares the object for use. It sets properties on the new object instance and invokes methods.
  • PostInitialization - This stage occurs just before ObjectBuilder returns the newly created object.
The application blocks that use ObjectBuilder add specific strategies to the pipeline, and you can add your own strategies if required.

Strategy Types
The following describe the default strategies for ObjectBuilder
  • TypeMappingStrategy - This strategy can set the actual return type for objects.
  • SingletonStrategy - This strategy specifies whether ObjectBuilder returns a new instance of an object or an existing instance if one is available.
  • ConstructorReflectionStrategy - This strategy inspects the class for attributes declared on constructors, in particular the [InjectionConstructor] attribute, and chooses which to use to construct the object.
  • CreationStrategy - This strategy instantiates the new object, using either the constructor or the Activator class methods.
  • PropertySetterStrategy - This strategy can set the value of public properties of the new object instance, based on policy.
  • PropertyReflectionStrategy - This strategy inspects the class for attributes on properties and applies these to the new object instance.
  • MethodReflectionStrategy - This strategy inspects the class for attributes on methods that must run during the Initialization stage.
  • MethodExecutionStrategy - This strategy executes methods on the new object instance, depending on policy.
  • BuilderAwareStrategy - This strategy inspects the class to see if it implements the IBuilderAware interface.
Attribute-based Dependency Injection
ObjectBuilder supports a general-purpose attribute-based dependency injection. Two of the built-in strategies, ConstructorInjectionStrategy and PropertyInjectionStrategy, provide dependency injection. Both of these strategies support a common set of attributes that you can apply to variables. These attributes are the following:

[CreateNew]. This attribute tells the dependency injection system to always create a new one of whatever it is you need. This is helpful for patterns such as Model-View-Controller (MVC) or Model-View-Presenter (MVP), where creating a view automatically generates a new controller/presenter.
[Dependency]. This is a general-purpose attribute with four optional parameters:
parameters are Name, NotPresentBehavior, CreateType, SearchMode.



Constructor, Property, and Method Injection
Three of the built-in strategies, ConstructorReflectionStrategy, PropertyReflectionStrategy, and MethodReflectionStrategy, use the [Dependency] and [CreateNew] attributes to control their behavior.
The ConstructorReflectionStrategy has two phases. First, it figures out which constructor to use. Second, it figures out how to satisfy those constructor parameters.
The PropertyReflectionStrategy looks for properties with the [Dependency] and [CreateNew] attributes and satisfies them appropriately (they must be public and have setters). It does not do anything with any properties that have no attributes applied on a class.
The MethodReflectionStrategy looks for methods that have the [MethodInjection] attribute applied and that execute during the initialization phase.


Working with ObjectBuilder
It includes
  • Creating new objects
  • Locating a service
  • Registering a service
  • Registering a constructor

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.

ObjectBuilder

The Composite UI Application Block uses ObjectBuilder to create objects and perform dependency injection, and the guidance package generates code that uses ObjectBuilder.
To support component independence, a component must be able to use another component without requiring a change to the source code of the component to be used. To support this, the Composite Web Application Block uses dependency injection to create component object instances.
Application does not create instances of another component by directly constructing an object (using new). Instead, the source code for a Composite Web Application Block application contains attributes such as CreateNew and ServiceDependency.
These attributes support the injection of objects at run time.
It supports a general-purpose attribute-based dependency injection. The attributes determine the type of object that the ObjectBuilder factory creates.