Deep Unity 1.x Dependency Injection container two: Initialize Unity

Source: Internet
Author: User

Unity initialization is primarily a registration type mapping and specifies its lifecycle.

In this article, we use an interface Idialer, an abstract base class that implements the interface Dialer, two specific class Buttontypedialer and Figureplatedialer classes that inherit from Dialer, and a Dialer that uses Telep Hone class.

Life Cycle Management

The management of life cycle begins because unity manages the creation and resolution of objects based on the lifecycle they need to specify when registering for the type.

Unity uses classes that inherit from the Lifetimemanager base class to decide how to save references to object instances and how to destroy objects. Unity has two classes for lifecycle management: ContainerControlledLifetimeManager and Externallycontrolledlifetimemanager, described below for each of these two classes. If these two types of administration are specified, they are processed as a single object, whether they are registered in Registertype or registerinstance.

The ContainerControlledLifetimeManager class is the object that specifies Unity container management registration, that is, the object's lifecycle is consistent with the unity container, destroying all of its managed objects when unity is destroyed. While

The Externallycontrolledlifetimemanager class specifies unity to keep only weak references to objects, and the preservation and destruction of objects is controlled by the outer of the Unity container.

If you do not need to process a single object, you can call the Registertype method without specifying its Lifetimemanager class, so that the Unity container will use temporary lifecycle management, which is to create a new instance of each request to get the object.

Registering type mappings with Registertype

Registertype contains multiple overloads that support generics, and also contains one by one of corresponding Non-generic overloads. Only its generic overloads are elaborated here, and Non-generic overloads correspond to them.

The Registertype registration type mapping can be the type that the registration interface or base class corresponds to, or you can register the type directly. For the former, only one type of interface or base class registration is given here, and the other is directly replaced.

Registertype<tfrom, Tto> ()

This method registers a default type mapping and creates a new instance for each request to get the object. For example, the following code:

In the code in the example above, the first two lines are registered for the type. Run this code with the following results:

The corresponding configuration file for this registration is as follows:

At the same time, modify the above code to the following code:

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.