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

No comments: