Spring.net Study notes (ii)

Source: Internet
Author: User
Tags compact

Properties, collaboration objects, automatic assembly and dependency checking

Set properties and collaboration objects for an object

There are two main ways of controlling inversion (dependency injection):

    • Attribute injection (by: The Set value method is injected,setter-based Dependency Injection, but for. NET, I'm afraid it's more appropriate to call it a property injection): After the object is created, the dependency is injected through the (called) property (the Set Value method). Spring.net recommends that you use attribute injection because the constructor's arguments are too many to make the code and object definitions of the class bloated, especially if some properties are optional (that is, not necessarily injected).

    • Constructor Injection (constructor-based Dependency Injection): Invokes a parameter constructor that injects a dependency through the constructor parameter when the object is created. Although Spring.net recommends using attribute injection as much as possible, it also fully supports constructor injection, because sometimes we can only use old types with parameter constructors and no properties defined. Also, for simpler objects, some people might prefer to use constructor injection to ensure that objects are in a valid state immediately after they are created.

The Iobjectfactory interface supports both methods of injection. In the container, we configure the dependencies through the XML object definition, and the container transforms with the type converter if necessary.

When dealing with object dependencies, Spring.net does several things:

  1. Creates and initializes a container based on the configuration information that contains the object definition. Most users use iobjectfactory or Iapplicationcontext implementation classes that support XML configuration files as containers.

  2. Each object's dependencies are configured in the object definition through a property or constructor. When an object is created, the container injects the dependency to the object.

  3. A property or constructor parameter can be set either as an actual value or as a reference to another object in the same container. If you use Iapplicationcontext as a container, you can also refer to objects in the parent container.

  4. The value that is configured for a property or constructor parameter must be able to be transformed to the actual type of the property or parameter. By default, spring.net can transform string values into arbitrary primitive types, such as Int,long,bool. In addition, XML-based containers can also use XML nodes to configure values for collection types such as IList, IDictionary, and set, and spring.net use TypeConverter to convert string values to any other type. Refer to 5.3, type conversion For more information about TypeConverter, and ways to automatically convert custom types using Spring.net. (Press: Set is the collection type provided by Spring.net)

  5. When the container itself is created, Spring.net verifies the configuration information for each object within the container and verifies that the object's dependencies are also valid (that is, objects that are referenced by objects are also defined in the same iobjectfacotry, or in the parent container for Iapplicationcontext). However, the assignment of a property only occurs when the object is created. For an object that is created in singleton mode (such as a singleton object defined in Iapplicationcontext), the creation of the object occurs when the container itself is created, otherwise (by: lazy creation, or non-singleton) occurs when the object is requested. When an object is created, it can cause a series of other objects to be created at the same time, because the dependencies of the object, and the dependencies of the dependency, need to be created and assigned at this time.

  6. In general, spring.net can do these things very well. When loading a container, spring.net handles problems that occur in the configuration, such as referencing a nonexistent object definition or looping dependencies, and so on. The setting of the property and the resolution of the dependency (that is, the behavior of creating all dependencies when needed) is deferred until the object is actually created. That is, if an object or its dependencies are not created correctly, even if the container can be created normally, an exception is thrown when the object is requested. For example, if an attribute that was supposed to be assigned is omitted, or if the property value given is invalid, an exception is thrown, causing the object to not be created correctly. This is also why Iapplicationcontext uses the non-inert singleton mode as the default deployment method. All objects are created before they are actually needed, and the overhead of this time and space guarantees that Iapplicationcontext will find the problem early when it is created. If you prefer, you can override this default behavior at any time and set any object to lazy creation.

First look at an example of attribute injection using an IOC container. Here is the XML object definition, followed by the code of the related type.

<ObjectID= "Exampleobject"type= "Examples.exampleobject, exampleslibrary">    < Propertyname= "Objectone"ref= "Anotherexampleobject"/>    < Propertyname= "ObjectTwo"ref= "Yetanotherobject"/>    < Propertyname= "Integerproperty"value= "1"/></Object><ObjectID= "Anotherexampleobject"type= "Examples.anotherobject, exampleslibrary"/><ObjectID= "Yetanotherobject"type= "Examples.yetanotherobject, exampleslibrary"/>

[C #] Public classExampleobject {PrivateAnotherobject Objectone; PrivateYetanotherobject ObjectTwo; Private inti;  Publicanotherobject Objectone {Set{ This. Objectone =value;} }     Publicyetanotherobject ObjectTwo {Set{ This. ObjectTwo =value;} }     Public intIntegerproperty {Set{ This. i =value;} }  }

The constructor parameters that are configured in the XML object definition are passed to the constructor of the Exampleobject class when the object is created.

Note that attribute injection (type 2) and constructor injection (type 3) are not exclusive and can be used simultaneously in the same object definition as follows:

<ObjectID= "Exampleobject"type= "Examples.mixediocobject, exampleslibrary">    <Constructor-argname= "Objectone"ref= "Anotherexampleobject"/>    < Propertyname= "ObjectTwo"ref= "Yetanotherobject"/>    < Propertyname= "Integerproperty"value= "1"/></Object><ObjectID= "Anotherexampleobject"type= "Examples.anotherobject, exampleslibrary"/><ObjectID= "Yetanotherobject"type= "Examples.yetanotherobject, exampleslibrary"/>

[C #] Public classMixediocobject {PrivateAnotherobject Objectone; PrivateYetanotherobject ObjectTwo; Private inti;  Publicmixediocobject (Anotherobject obj) { This. Objectone =obj; }     Publicyetanotherobject ObjectTwo {Set{ This. ObjectTwo =value;} }     Public intIntegerproperty {Set{ This. i =value;} }  }

Now consider an alternative to a constructor. In the following example, Spring.net uses the static factory method to create the object:

<ObjectID= "Exampleobject"type= "Examples.examplefactorymethodobject, exampleslibrary"Factory-method= "CreateInstance">    <Constructor-argname= "Objectone"ref= "Anotherexampleobject"/>    <Constructor-argname= "ObjectTwo"ref= "Yetanotherobject"/>    <Constructor-argname= "Intprop"value= "1"/></Object><ObjectID= "Anotherexampleobject"type= "Examples.anotherobject, exampleslibrary"/><ObjectID= "Yetanotherobject"type= "Examples.yetanotherobject, exampleslibrary"/>

[C #] Public classexamplefactorymethodobject{PrivateAnotherobject Objectone; PrivateYetanotherobject ObjectTwo; Private inti; //a private constructor  PrivateExamplefactorymethodobject () {} Public Staticexamplefactorymethodobject CreateInstance (anotherobject objectone, Yetanotherobject ob Jecttwo,intIntprop) {Examplefactorymethodobject FMO=NewExamplefactorymethodobject (); Fmo. Anotherobject=Objectone; Fmo. Yetanotherobject=ObjectTwo; Fmo. Integerproperty=Intprop; returnFMO; }  //Property Definitions}

Note in the object definition, the parameters required by the static factory method are also configured through the Constructor-arg node, and the constructor is used directly. Note that the type of the factory method product object does not have to be a type that contains a factory method. The method of configuring the instance factory method is basically the same as the static factory method (except to use the Factory-object attribute instead of the type attribute), so no more.

Spring.net Learning Note (b)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.