Unity (6): Use Case III: for dependency injection (I)

Source: Internet
Author: User

Address: http://www.cnblogs.com/kebixisimba/archive/2008/05/14/1196942.html

 

Where did we go...
    • Unity (1): Starting from objectbuilder
    • Unity (2): What is unity?
    • Unity (III): Quick Start
    • Unity (iv): Application Scenario I: Build type ing
    • Unity (V): Use scenario II: For Singleton Mode

 

Dependency Injection

What is dependency injection? I think this problem is not suitable for me here, so I recommend it to the master.Article. As mentioned in previous articles, "Martin Fowler's Inversion of control containers and the dependency injection pattern have to be recommended. Of course, if it is hard to read, do you really understand IOC and AOP? Series is also a good reference ".

The dependency injection and inversion of control are not the same concept, and some people are often mixed together. Take a look at Martin's article.

 

Preparations

For the following description, we first compile Several Interfaces and classes that are later required.

Note: The iorder interface, commonorder class, viporder class, ilogger interface, consolelogger class, And nulllogger class involved in this article Code For details, see unity (3): Quick Start and Unity (5): Use Case II: Prepare code in singleton mode.

Iorderwithlogging interface:

 

Unity supports three types of dependency injection: constructor injection, set-value injection, and method call injection.

Constructor Injection

For constructor injection, unity supports two methods of dependency injection: Automatic dependency injection and tag identification. Specifically, for Target classes that only have a single constructor, you can use the automatic dependency injection method. For Target classes that have multiple constructor functions, injection should be performed by labeling the constructor.

    • Automatic Single constructor Injection

The following is our target class singleconstructor. We can see that the singleconstructor class has only one constructor, which has two parameters: iorder and ilogger.

The following is our test code. First, we create a container, then register the iorder and ilogger classes in the container, and register singleconstructor as the specific class of iorderwithlogging.

Here, we register commonorder as a singleton and assign the discount value to 0.8 for its Singleton instance, the purpose of this operation is to verify that the specific class of the retrieved iorder is the commonorder class we injected to the container previously.

The following is the output result.

 

    • Multi-constructor tag Injection

The following is our target class multipleconstructor. We can see that the multipleconstructor class has two constructors, one without parameters and the other with two parameters: iorder and ilogger. The latter has the injectionconstructor label.

The following is our test code. Similar to the above example, here we replace the specific classes of iorder and ilogger with viporder and nulllogger, and the specific classes of iorderwithlogging with multipleconstructor.

The following is the output result.

Note that if we do not use the injectionconstructor label in the above multipleconstructor class, the running results will be the same. You can test it on your own.

This is because for Target classes with multiple constructors, if no constructor labeled with injectionconstructor is found, unity will automatically use the constructor with the most parameters. If you find that the constructor with the most parameters has multiple constructors at the same time, an exception will be thrown during runtime. For example, if the constructor class in our multipleconstructor class is not labeled as injectionconstructor, and there is also a constructor with two parameters, the result is as follows.

 

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.