Go deep into unity 1.x dependency injection container 2: Initialize unity

Source: Internet
Author: User

Link: http://www.doriandeng.cn/archives/97.html

 

 

Unity initialization mainly registers the type ing and specifies its lifecycle.

In this article, we use an idialer interface, an abstract base class dialer that implements the interface, and two concrete classes inherited from dialer, buttontypedialer and figureplatedialer, and a telephone class that uses dialer.

Lifecycle Management

The reason for putting lifecycle management at the beginning is that unity manages object creation and parsing Based on the lifecycle that needs to be specified during type registration.

Unity uses classes inherited from the lifetimemanager base class to determine how to save object instance references and how to destroy objects. Unity comes with two classes for Lifecycle Management: containercontrolledlifetimemanager and externallycontrolledlifetimemanager. The two classes are described below. If the two management methods are specified, both the registertype and the type registered with registerinstance are processed as single-piece objects.

The containercontrolledlifetimemanager class specifies the objects registered by the Unity container management. That is, the object lifecycle is the same as that of the Unity container. When the unity container is destroyed, all the objects managed by it are destroyed. At the same time,

Externallycontrolledlifetimemanager class specifies that unity only keeps weak references of objects. The storage and destruction of objects are controlled by the outside of the Unity container.

If you do not need to process a single-piece object, you can call the registertype method without specifying its lifetimemanager class, so that the unity container uses temporary lifecycle management, that is, a new instance is created for each request to obtain an object.

Use registertypeRegistration type ing

Registertype contains multiple overload types that support generics, and also contains a one-to-one non-generic overload. Here, we will only elaborate on its generic overload, which corresponds to its non-generic overload.

Registertype registration type ing can be the type corresponding to the registration interface or base class, or you can directly register the type. For the former, only one type of interface or base class registration is provided here, and the other can be directly replaced.

Registertype <tfrom, bytes> ()

This method registers a default type ing and creates a new instance for each request to obtain the object. For exampleCode:

In the code in the above example, the first two rows are registration of types. Run this code and the result is as follows:

The corresponding configuration file for such registration is as follows:

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

Run this code and we will get the same result.

Registertype <tfrom, memory> (lifetimemanager lifetime)

This method differs from the previous method in that you can specify the lifecycle management. For example, we have the following code:

The following result is displayed:

It can be seen that after containercontrolledlifetimemanager is used, the same object is obtained twice, that is, after the unity container creates an object at the first acquisition, it is saved for subsequent acquisition.

Then, we split the statements into two methods, as shown below:

In this case, we can get the same result as the previous one. You can replace the containercontrolledlifetimemanager class with the externallycontrolledlifetimemanager class to check the different effects of these two classes.

To use the configuration file to complete registration, we can use the following Configuration:

At the same time, we need to modify the above 64th lines of code 22nd and 23 lines of code to apply the configuration file.

Registertype <tfrom, struct> (string name)

This method is used to register a named type ing instead of the default type registration. If name is null, it will act the same way as registertype <tfrom, delimiter>. The sample code is as follows:

From the code above, we can see that the difference of this method is that you need to specify the type registration name when getting the object. The corresponding configuration file is as follows:

Registertype <tfrom, memory> (string name, lifetimemanager lifetime)

This method is a combination of the preceding three methods, which will not be detailed here.

Others

Other methods include registertype <t> (lifetimemanager lifetime) and registertype <t> (string name, lifetimemanager lifetime.

Use registerinstanceRegister an existing object instance

Registerinstance is used to register a created object instance as a single-piece type ing. By default, containercontrolledlifetimemanager is used for lifecycle management.

Registerinstance <t> (T instance)

This method registers a default single-piece type ing. The sample code is as follows:

In this example, we first create a buttontypedialer type object, register it as a dialer type ing, and then get it through unity.

Unlike registertype, its configuration file is as follows:

In the configuration file, we use a dialerconverter class, which is a type conversion class. The value element is required in the instances element. To create a custom instance, A proper type conversion class is required to convert the value represented by a field string to the required type. Accordingly, our code is modified as follows:

Note: the type specified in the resolve method must be the same as that in the configuration file; otherwise, an exception is triggered.

Registerinstance <tinterface> (tinterface instance, lifetimemanager lifetime)

This method specifies a lifecycle management when registering the type ing of an existing object instance. The sample code is as follows:

You can replace containercontrolledlifetimemanager with externallycontrollerlifetimemanager. An exception occurs.

The configuration file corresponding to this method is, unfortunately, the configuration file does not support the lifecycle configuration of this method.

Others

Other methods include registerinstance (string name, tinterface instance) and registerinstance (string name, tinterface instance, and lifetimemanager lifetime.

UnityContainer hierarchy

The Unity container supports the nesting of containers. Under what circumstances do you need to use the nesting of containers. For example, if you register multiple types of mappings for the same type, you need to use nesting when the type ing requires different lifecycle management. The following describes how to register multiple types of mappings for the same type.

The sample code is as follows:

Run the code and we can get the following results:

Some may think that it is better to directly create two containers, but the reason is that all objects can be managed in the parent container, sub-containers are mostly used to manage some temporary mappings. In addition, you can use the ing registered in your parent container in the sub-container, that is, the container will search and register it on its own.

The Unity configuration file does not directly support container nesting. In this case, we can define the parent container as the default container in the configuration file and configure the sub-container as the named container, then, apply the name configuration to the sub-container. For example, we have the following Configuration:

By modifying the Code as follows, we can get the same result:

Summary

Through the above learning, we have fully understood the initialization of the Unity dependency injection container. After these different registrations, we can get the object and use the object in different ways.

Source codeDownload

Look-into-unity-1-1-initunity.zip

Hope to help you!

Deng Ming

Ideal & Beauty

Related Article

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.