Microsoft Enterprise Library unity Study Notes (1)

Source: Internet
Author: User

This article mainly introduces:

About unity container configuration, registration of ing relationships, types, single instances, existing objects, and some basic container configurations, this is just my learning experience and notes on unity, I hope you can learn more from each other through communication. Thank you for your support.

You can create a unity ing for unity iner using the following two methods:

    • Registertype: This method registers a type or ing relationship to the container. When we need to call this type of instance, the container will automatically instantiate this type of object, you do not need to instantiate an object using the new somename method (for example, useResolveOrResolveallMethod to obtain the registered instance). If the life cycle of the instantiated object is not specified, the defaultTransientlifetimemanager(Each callResolveOrResolveallMethod ).
    • Registerinstance: This method registers an existing instance in the container.ResolveOrResolveallMethod to obtain this type of instance, which is used by default.ContainercontrolledlifetimemanagerManage the object lifecycle, and the container maintains the object reference (in short, each callResolveOrResolveallMethods call the reference of the same object ).

1.1 register an interface or class ing to a specific type:

You can use the Register Method to register a type or ing relationship to the container.

    • L register a type in the containerRegistertype<Type>
    • L register a ing relationship to the containerRegistertype<Registeredtype, targettype>

CallRegistertypeThe <registeredtype, targettype> () method can register a ing relationship with the container. registeredtype is an interface or class, And targettype must implement this interface or inherit this class.

1.2 register a singleton or Singleton instance:

Life Time managers ):

    • L use it by defaultTransientlifetimemanagerManage the lifecycle of an object. The effects of the following two methods are the same:
Container. registertype<Imyservice, customerservice>();

Container. registertype<Imyservice, customerservice>(NewTransientlifetimemanager ());
    • L useContainercontrolledlifetimemanagerCreate a singleton instance
Container. registertype<Imyservice, customerservice>(NewContainercontrolledlifetimemanager ());
    • L useExternallycontrolledlifetimemanagerOnly weak references of objects can be maintained.
Container. registertype<Imyservice, customerservice>(NewExternallycontrolledlifetimemanager

Strong references and weak references:

Strong reference: When a root points to an object, the object cannot be spam, becauseProgramCodeStill accessing this object.

Weak reference: allows the Garbage Collector to collect objects and allow applications to access the objects. The execution result depends on the time.

    • LUsePerthreadlifetimemanagerThe same object instance is returned in the same thread, and different thread object instances are different. 

 

 
Container. registertype<Imyservice, customerservice>(NewPerthreadlifetimemanager ());

 

In useRegistertypeRegistration type or ing. If the object lifecycle is performed, the defaultTransientlifetimemanager, It does not save the object reference in the container, in short, every callResolveOrResolveallMethod Will instantiate a new object. When we want to save the object reference in the container, we can use containercontrolledlifetimemanager to manage the object lifecycle. In short, each callResolveOrResolveallMethods call the reference of the same object.

1.3 register an existing object as a single-piece instance:

Register an existing object. We useRegisterinstance, Which is used by defaultContainercontrolledlifetimemanagerTo manage the lifecycle of an object, review the following:

Difference between registertype and registerinstance:

Method

Default lifetime managers

Same lifetime managers

Registertype

Transientlifetimemanager

Containercontrolledlifetimemanager

Registerinstance

Containercontrolledlifetimemanager

Containercontrolledlifetimemanager

WhenRegistertypeAndRegisterinstanceBoth useContainercontrolledlifetimemanagerTime Difference:

Msdn: if you usedRegistertypeMethod to register a type, unity creates a new instance of the registered type during the first call toResolveOrResolveallMethod or when the dependency mechanic injects instances into other classes. Subsequent requests return the same instance.

If you usedRegisterinstanceMethod to register an existing object, unity returns this instance every time you callResolveOrResolveallMethod or when the dependency mechanic injects instances into other classes.

Translation: If you useRegistertypeMethod registration type, when the first callResolveOrResolveallWhen a method or dependency mechanism injects an instance to another class, unity will instantiate the registration type, and the same instance will be returned for subsequent calls.

If you useRegisterinstanceMethod to register an existing object. When we callResolveAndResolveallWhen a method or dependency mechanism injects an instance to another class, unity returns this object instance every time.

1.4 Use the fluent interface attribute of container:

This means that you can use the method chain in a statement.

   iunitycontainer mycontainer  =    New   unitycontainer () 
. registertype imyservice, dataservice > ()
. registertype imyutilities, dataconversions > ()
. registerinstance imyservice > (myemailservice );

BecauseIunitycontainerInterface implementationIdisposeInterface, all of which can be usedUsingControl container resource release

 

    ///  /iunitycontainer implements the interface idisposable, therefore, you can use 
/// /using effectively releases the system resources occupied by objects.

using (iunitycontainer container = New unitycontainer ())

{< br> /// /implementation

}

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.