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.