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: