Deep Unity 1.x Dependency Injection Container IV: Dependency Injection

Source: Internet
Author: User
Tags constructor

Unity is a dependency injection container, and natural dependency injection is its most important function. From previous discussions, contradictions have focused on unity's intrusion into the object of dependence, which may be the unity in the preview version of the document did not say, plus a foreign cow of an article caused by the consequences. Because Unity can also support non-invasive dependency injection through configuration files. This article describes the support for Unity dependency injection from the use of Attribute and configuration files in two aspects.

Unity supports constructors, properties, and method invocation injection.

Constructor injection

Constructor injection contains two cases, one is the class has only one constructor, Unity can be automatically injected; Alternatively, when a class contains more than one constructor, it must use Attribute or a configuration file to specify the constructor to use when injected.

Suppose you now have a telephone class that contains only a constructor with a parameter of type Idialer.

As shown below:

So, we can use this:

At this point, we do not need to make any configuration of the telephone.

Then, add a constructor with the string type parameter (note that this constructor is not useful), 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.