Copyright (c) 2007 Dorian Deng. Signature Sharing is not commercially available. 
 
Reprinted please indicate the source: http://www.cnblogs.com/doriandeng/archive/2007/10/07/916431.html 
 
 
 
 
Enterprise Library uses a custom underlying
 ObjectbuilderThe system injects an instance of the appropriate type of object at runtime (the instance has a preset attribute) to the application.Program.
Objectbuilder Design
 
 
 
The construction and release of object instances is a common process in most applications, especially when Enterprise Library is used.
Built in commercial applications. For this reason, Enterprise Library
The Application Block usesObjectbuilerIt executes all repetitive and necessary tasks for creating object instances, and provides high flexibility.
 
 
 
Objectbuilder
Encapsulates the creation of object instances. It can do the following:
 
 
 
  
  Create a specific class instance, or even an abstract type.
  
  If possible, an existing object instance is returned, or a new instance is always generated.
  
  Create an appropriate object from the factory class based on configuration data.
  
  When a class exposes multiple constructors, You can intelligently select the appropriate constructors.
  
  Automatically apply values to common attributes and execute object methods in response to predefined policies.
  
  Respond defines attributes on attributes and methods. It affects the creation and naming of new objects.
  
  Automatic and supportIbuilderaware
 Interface to indicate that the object has been created.
  
  The destroy function is provided, which allows the reverse operation chain to remove settings from existing objects.
  
 
 
 
 
Objectbuilder
It is a bottom-layer tool that does not directly interact with it in most cases. Use the class in the Application Block to generate an object instance when creating a class declared in the Application Block. However, you can directly useObjectbuilder
 . You can also create your own policies or modify existing policies to influenceObjectbuilder
 .
 
 
 
AboutObjectbuilderFor more information, including documentation and release updates, see
Objectbuilder dependency injection framework.
 
Objectbuilder Architecture 
Objectbuilder
 APolicyMPs queue. With this pipeline, multiple operations can be replaced with objects described and ready for use. This means that the order of replacement can be controlled. It also supports release of controlled objects that run appropriate MPs queues in reverse order.
 
 
 
Objectbuilder
 The process of managing object execution during policy construction and destruction.Objectbuilder
 An MPs queue is organized into multiple stages. Each stage of the MPs queue contains multiple policies. You can implement your own policy as a. net
Framework class with the specified interface.
 
Objectbuilder Method 
 
ObjectbuilderExist in namespace
 Microsoft. Practices. objectbuilderIts base class
 BuilderbaseTwo methods are exposed.BuildupThere are two overload methods.
 
 
 
Example 3.9. C #
 
 
 Buildup (locator, type, ID, instance, policies []); buildup <type> (locator, ID, instance, policies []);
 
 
 
 
 
Example 3.10. Visual Basic. net
 
 
 Buildup (locator, type, ID, instance, policies () buildup (of type) (locator, ID, instance, policies ())
 
 
 
 
 
 
PreviousCodeContains the following elements:
 
 
 
  
  -  Locator 
  
  
  -  
   
This is an implementationIreadwritelocator
 Class reference of the interface, which provides clues for the placement of objects or classes. For more information, see "Use
Readwritelocator ".
 
    
  -  Type 
  
  
  -  
   
Type of the object to be created.
 
    
  -  ID 
  
  
  -  
   
ID assigned to the new object instance.
 
    
  -  Instance 
  
  
  -  
   
Optional. Reference to an existing object instance in the pipeline process that will take effect. This allows the use of existing objects and confirms that they are transmitted through the pipeline to prepare for use as appropriate.Objectbuilder
 Only suitable processes are applied to such object instances.
 
    
  -  Policies [] 
  
  
  -  
   
PolicylistInstance array, which implements the policy
), Covering the built-in policies. For more information, see "Use
 Policylist".
 
    
 
 
 
 
TeardownMethod to use an existing object instance and run it after passing through the Policy chain. This process can be removed fromBuildupFeatures added to the object during the process, if you want to re-use the object.
 
 
TeardownThe method signature is as follows.
 
 
 
Example 3.11. C #
 
 
 Public titem teardown <titem> (ireadwritelocator locator, titem item)
 
 
 
 
 
 
Example 3.12. Visual Basic. net
 
 
 Public Function teardown (of T) (byval locator as ireadwritelocator, byval item as t) as T _ implements ibuilder (of tstageenum). teardown
 
 
 
Use readwritelocator
 
 
 
ObjectbuilderUse the lifecycle Package concept to save object references, which ensures correct Object Management and Destruction at the right time. The locator references an independent object in the lifecycle package. Multiple entries in the positioner can point to the same object instance. For example, entries in the locator may
It is referenced by the class type name, the interface it implements, and the object name.
 
 
The positioner can be read-only or read/write,ObjectbuilderOf
 Buildup
The method utilizes an implementationIreadwritelocator
 The instance of the object.
 
Use policylist 
 
PolicylistIs a policy (Policy
), Each of which is implementedIbuilderpolicyInterface classes, which affect
 ObjectbuilderObject creation method. The policy can be temporary or permanent.
 
 
 
The temporary policies areObjectbuilder
 Or attributes declared in the class file through reflection)
Policy.
 
 
 
Persistent policies are implementedIbuilderpolicy
 Interface. You can pass the array of these class instancesObjectbuilderOfBuildup.
 
MPs queue stage 
 
InObjectbuilderThe MPs queue consists of the following four stages:
 
 
 
  
  -  Pre-create 
  
  
  -  
   
Occurs inObjectbuilderBefore creating a new instance of an object.
 
    
  -  Create 
  
  
  -  
   
In this phase,ObjectbuilderCreate a new object instance.
 
    
  -  Initialization 
  
  
  -  
   
In this phase,Objectbuilder
Prepare objects for use. It sets the attributes of the new object instance and calls the method.
 
    
  -  After Initialization 
  
  
  -  
   
This phase occurs inObjectbuilderBefore returning the new object.
 
    
 
 
 
 
Table 3.1 listsObjectbuilderDefault processing completed in each process of the MPs queue.
 
 
 
Table 3.1. Procedure for starting a composite UI block Application
 
 
 
  
   
   
  
  
   
   | MPs queue steps |  
   Objetbuilder Policy |  
  
 
  
  
   
   | Procreation) |  
   Typemappingstrategy; singletonstrategy; Constructorreflectionstrategy |  
  
 
   
   | Creation) |  
   Creationstrategy |  
  
 
   
   | Initialization) |  
   Propertysetterstrategy; propertyreflectionstrategy; Methodreflectionstrategy; methodexecutionstrategy |  
  
 
   
   | Post-initialization (postinitialization) |  
   Builderawarestrategy |  
  
 
  
 
 
 
 
 
Application Block usageObjectbuilder
 Add the specified policy to the MPs queue. If necessary, you can add your own policy.
 
Policy type 
 
Used in Table 3.2Objectbuilder
 The default policy is as follows:
 
 
 
 -  
 
 
 -  Typemappingstrategy 
 
   
 -  
 
 
 -  
  
This policy can design the object's actual return type. For example, an abstract or interface typeImyobject
 Is automatically forced to convert to a specific type.Myobject.
 
    
 -  
 
 
 -  Singletonstrategy 
 
   
 -  
 
 
 -  
  
This policy indicatesObjectbuilder
 Returns a new object instance or an available instance.
 
    
 -  
 
 
 -  Constructorreflectionstrategy 
 
   
 -  
 
 
 -  
  
This policy checks the classes of attributes declared in the constructor, especially[Injectionconstructor]
 Attribute, and select which one is used for object creation. Usage in Constructor[Dependency]
 And[Createnew]
More information about how Attribute-modified parameters affect behavior is described later in this section.
 
    
 -  
 
 
 -  Creationstrategy 
 
   
 -  
 
 
 -  
  
This policy is used to create new objects, using constructors orActivator
 Class method.
 
    
 -  
 
 
 -  Propertysetterstrategy 
 
   
 -  
 
 
 -  
  
This policy can be used to set the public attribute value of the new object instance based on the policy.
 
    
 -  
 
 
 -  Propertyreflectionstrategy 
 
   
 -  
 
 
 -  
  
This policy checks attributes on properties)
And apply them to the new object instance. Yes[Dependency]And
 [Createnew]Attribute attributes)
Application causes these values to be injected into the new object instance.
 
    
 -  
 
 
 -  Methodreflectionstrategy 
 
   
 -  
 
 
 -  
  
This policy checks the attribute classes on the method. The method must run during the initialization phase.
 
    
 -  
 
 
 -  Methodexecutionstrategy 
 
   
 -  
 
 
 -  
  
This policy runs the methods in the new object instance, depending on the policy ).
 
    
 -  
 
 
 -  Builderawarestrategy 
 
   
 -  
 
 
 -  
  
This policy checks the class to see if it implementsIbuilderaware
 Interface. If yes,Objectbuilder
 The notification object is triggered when the creation is complete.Onbuiltup
 It is ready for use after preparation. IfObjectbuilderRunning
 TeardownMethod, it will triggerOntearingdown
 Event.
 
    
 
 
If you want to create more steps, modify or remove the current steps, you canBuilderbase
 Class to derive your own class, and use your own steps, or pre-initialize a constructor to meet your needs. View the source file of the constructor (
 Builder. CS) You can see how to do this.
 
Attribute-based dependency Injection 
 
Objectbuilder
 Supports a usable attribute-based dependency injection. Two built-in policies,Constructorinjectionstrategy
 AndPropertyinjectionstrategy
, Provides dependency injection. Both policies support a common property set and can be applied to variables. These attributes are as follows:
 
 
 
  
  -   [createnew]  
  
  
  -  
   
 This attribute indicates that the dependency injection system always creates new requirements. This is useful for design patterns like Model-View-controller (MVC) or Model-View-Display Device (MVP, A new Controller/display is automatically generated when a view is created in these places. 
 
    
  -   [dependency]  
  
  
  -  
   
 This is a general purpose attribute with four optional parameters: 
 
    
    -   name  
    
  
    -  
     
 the specified named object replaces the unnamed object. The default value is 
   null   (untitled ). 
 
      
    -   notpresentbehavior  
    
  
    -  
     
 specify what will happen if the desired object does not exist. The default value is 
   notpresentbehavior. createnew  . 
 
      
    -   createtype  
    
  
    -  
     
 specify the type to be created. 
 
      
    -   searchmode  
    
  
    -  
     
 specify whether the positioner will be an object search tree or eliminate dependencies. If omitted, the default value is   up  . 
 
      
   
 
    
 
 
 
In front[Dependency]
 These attributes in properties mean that they can meet all of the following requirements:
 
 
 
  
  Search for unnamed x (x is a type ). If it does not exist, an exception is thrown.
  
  Search for unnamed X. If it does not exist, a new one will be created.
  
  Search for unnamed X. If it does not exist, provide a null value instead.
  
  Search for the X (Y is a string) named Y ). If it does not exist, an exception is thrown.
  
  Search for X named Y. If it does not exist, create a new one.
  
  Search for X named Y. If it does not exist, a null value is provided.
  
 
 
 
 
For example, the following code always generates a type:Mycustomobject.
 
 
 
Example 3.13. C #
 
 
 Using Microsoft. Practices. objectbuilder; [dependency (createtype = typeof (mycustomobject), notpresentbehavior = notpresentbehavior. createnew)] public icustomobject mycustomobject {set {...}} 
 
 
 
 
 
Example 3.14. Visual Basic. net
 
 
Imports Microsoft. practices. objectbuilder <dependency (createtype: = typeof (mycustomobject), notpresentbehavior: = notpresentbehavior. createnew)> _ Public writeonly property mycustomobject () as icustomobject set... end setend Property
 
 
 
 
 
 
If the system does not findIcustomobjectIt will create a new
 MycustomobjectAnd
 Icustomobject
 Intended UseIcustomobject
 Type.
 
Constructor, attribute, and method Injection 
 
Three built-in policies,Constructorreflectionstrategy
 ,PropertyreflectionstrategyAnd
 Methodreflectionstrategy, Use[Dependency]
 And[Createnew]Attribute to control their behaviors.
 
 
Constructorreflectionstrategy
 There are two aspects. First, it depicts the constructors used; second, it depicts how to satisfy the parameters of those constructors.
 
 
 
ConstructorreflectionstrategyFirst, find the application
 [Injectionconstructor]
 Any constructor of an attribute (this can only be one of them). If it is found, use this constructor. If no application is available[Injectionconstructor]
Only one constructor is used. If there are multiple constructors, but no application is available
 [Injectionconstructor]Attribute. The policy throws an exception.
 
 
 
After the constructor is selected, the policy determines how parameters are required to the constructor.[Dependency]
 Or/and[Createnew]
 Attribute tag. Any parameter without application properties is processed as the default[Dependency]
 Attribute (untitled,Notpresentbehavior =
Notpresentbehavior. createnew, searchmode = up).
 
 
PropertyreflectionstrategySearch
 [Dependency]And[Createnew]Property (
Attribute) and meet them as appropriate (they must be public and have a configurator ). In a class, it does not have to have no application attribute (
Attribute.
 
 
 
MethodreflectionstrategyFound Application
 [Methodinjection]
 And executed in the initialization phase. Parameters of these methods may include[Createnew]And
 [Dependency]Attribute. Any parameters without application properties will be
The default[Dependency]
 Attribute (untitled,Notpresentbehavior =
Notpresentbehavior. createnew, searchmode = up).
 
Enterprise Library attributes) 
 
Enterprise Library provides its own policiesObjectbuilder
 . These policies are as follows:
 
 
 
  
  -  Configurationnamemappingstrategy 
  
  
  -  
   
This policy reads the configuration source and converts itObjectbuilder
 The format that can be used.
 
    
  -  Singletonstrategy 
  
  
  -  
   
This policy RestrictionObjectbuilder
 The number of object instances that can be created.
 
    
  -  Configuredobjectstrategy 
  
  
  -  
   
This policy commandObjectbuilder
 How to use configuration data and metadata configuration objects.
 
    
  -  Instrumentationstrategy 
  
  
  -  
   
This policy specifies a measuring instrument, such as a performance counter. Once an object is created,Objectbuidler
 Attaches a measurement to an object.
 
    
 
 
 
 
TheseObjectbuidler
 The policy uses the correct attributes to create objects. Table 1 lists some of the most important attributes.
 
 
 
Table 3.2. Enterprise Library attributes)
 
 
 
  
   
   
  
  
   
   |  attribute  |  
    description  |  
  
 
  
  
   
   |  configurationelementtype  |  
    specify the configuration object type that  objectbuilder    will be used to create an object. The type must be derived from    nametypeconfigurationelement    and associated with the attribute class.  |  
  
 
   
   |  customfactoryattribute  |  
    specify the custom factory type that    objectbuilder  will be used to build the object.  |  
  
 
   
   |  configurationnamemapper  |  
    specifies the type used to map the XML element name to the provider level. The specified type must be implemented by the    iconfigurationnamemapper    interface,  configurationnamemappingstrategy    use this attribute to find the type to be used when ing names.  |  
  
 
   
   |  receiver  |  
    define the factory of the build object.  |