Unity (7): Use Case III: for dependency injection (II)

Source: Internet
Author: User

Address: http://www.cnblogs.com/kebixisimba/archive/2008/05/26/1207503.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
    • Unity (6): Use Case III: for dependency injection (I)

 

Fluent Interface

PreviousArticleI used the fluent interface feature, but I forgot to explain it. Sorry... make up!

First look at the SectionCode:

It may seem a little unaccustomed, but if you look at it carefully, you will feel that your mind is clear and you can read this code smoothly. This feature is called fluent interface (I still don't know what a unified Chinese translation is). For details, see Martin Fowler's http://martinfowler.com/bliki/fluentinterface.html. this concept was first proposed by Martin Fowler.

 

The following describes the other two dependency injection methods supported by unity.

Set Value Injection

The following is our target class propertyinjection. We can see that both the attributes myorder and mylogger in the propertyinjection class are labeled with dependency. The Unity container injects the required objects into the target Class Based on the labels on these attributes.

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

Similarly, here we register commonorder as a singleton and assign a value of 0.95 to the discount of a singleton, 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.

 

Method call Injection

The following is our target class methodcallinjection. This time, the injectmethod label is applied to the method inject. The unity container will inject the required objects to the target Class Based on the labels on these methods.

The following is our test code. Similar to the previous one, it is no longer repeated.

The following is the output result.

 

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.