Go deep into unity 1.x dependency injection container 4: dependency Injection

Source: Internet
Author: User

Original article: http://www.doriandeng.cn/archives/100.html.

 

Unity is a dependency injection container, and natural dependency injection is its most important function. From the past discussions, the contradiction has been concentrated on the dependency objects infiltrated by unity. This may be because unity is not described in the pre-release documents, plus an article by a foreign cool man.Article. Because, unity can also support non-intrusive dependency injection through the configuration file. This article describes how to use Attribute and configuration file to support unity dependency injection.

Unity supports constructor, attribute, and method call injection.

Constructor Injection

Constructor injection contains two situations: one is that unity can perform automatic injection when there is only one constructor in the class; the other is that when the class contains multiple constructor, attribute or configuration file must be used to specify the constructor used for injection.

Assume that there is now a telephone class that contains only one constructor with parameters of the idialer type.

As follows:

Then, we can use it like this:

In this case, you do not need to configure telephone.

Then, add a constructor with a string type parameter (note that this constructor is useless), as shown below:

If we useCodeThe system will throw a resolutionfailedexception exception because unity cannot parse which constructor to use to generate objects. However, if one of the constructor functions is the default constructor, unity automatically selects the constructor with parameters.

In this case, we need to specify the constructor to be used for unity by adding the injectionconstructor feature to the constructor. As follows:

In this way, unity can correctly select constructors and create new objects.

Next, let's take a look at using the configuration file to deliver the same effect. Add an application to the project first.ProgramAdd the following code to the configuration file:

From the above, we can see that the dependency injection configuration is specified through the typeconfig element, and its sub-element constructor specifies the constructor dependency injection, which is indicated by the element Param, the constructor with the parameter type idialer will be used. In addition, the dependency element specifies that the value of this parameter uses dependency injection. In this way, unity automatically creates an object instance based on the Type ing registered in the container as the constructor. The dependency element can be replaced by the value element to directly specify a constant.

Then, modify the Customer Code as follows to apply the Configuration:

Run the code and we will get the same result.

Property injection (setter)

Property injection is used to automatically create a value of the specified type for a specific property of the target object.

In the preceding telephone class, there is a dialer type attribute named dialer. The Dialer class is an abstract class. Shows the dialer attribute of Telephone:

Then, we use the following code to use it. (Note: The code registers two type mappings. The parameters of the dialer, idialer, and telephone constructor are of the idailer type, while the dialer attribute is of the dialer abstract class ):

Run the above code and we can get the following results:

You may have discovered that this is not the expected result. The Dialer class ing type should be the buttontypedialer class, rather than the figureplatedialer type in the result. That is to say, unity only applies constructor dependency injection without application attribute injection. Why? Because property injection must be specified by a feature or configuration file, unity can only apply it.

We add the dependency feature to the dialer attribute of telephone, as shown below:

Run the above Code again and we can see that the output is buttontypedialer. Note: Here, unity applies constructor injection and property injection at the same time. Why do we see the result of property injection? Constructor injection is applied before property injection. If no object is created, property injection cannot be applied to attributes of non-existing objects.

Like constructor injection, we can use the configuration file to specify the property injection. You can add the property element to the typeconfig element in the configuration file as follows:

Note that propertytype is used in this type, rather than parametertype used in the constructor. If default type ing is used, the dependency element can also be ignored. If you need to specify the name ing, you can use the dependency name attribute to specify. The dependency element can also be replaced by the value element.

Run the following code to get the expected results:

Method call Injection

Method call injection instructs unity to call the method specified by the object after creating the object to perform the specified initialization and other operations.

Method call injection is relatively simple. If you use the attribute method, you can use the injectionmethod feature to identify the method. For example:

The corresponding configuration file is as follows:

Does it look familiar? By the way, it is similar to the configuration of constructor injection. The only difference is that the name of the called method needs to be specified for method call injection.

Summary

Unity also supports constructor injection, property injection, and method call injection. You can use the attribute feature to intrude the service class, or use the configuration file to do not intrude into the service class.

Source codeDownload

Dependencyinjection.zip

Hope to help you!

Deng Ming

Ideal & Beauty

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.