Control inversion is basically about how the function caller interacts with the implementation of the feature, that is, there is no direct strong coupling between the two (the caller is new to the callee), but instead all rely on the same abstraction, which specifies the interface between the two. Inversion means the implementation of dependency inversion, in the program is not based on the lower layer of the interface to write the call, but upside down, the upper layer as needed to define the interface, the lower level is responsible for implementing this interface. This embodies the top-down design approach and bottom-up implementation, so that the software code has a natural readability, as well as the separation of concerns.
IOC container is the framework of implementation of the IOC, including the interface and implementation of the relationship between the configuration function and the ability to obtain the implementation of the object, where the function of obtaining the implementation of the object roughly corresponds to the factory pattern in the design pattern, the following brief analysis of the original ecological new call, Factory mode call, and IOC container call:
For the sake of comparison, it is assumed that the function is implemented by a specific class before analyzing the original ecological call. Direct invocation has the best performance, but maintainability and testability is poor, such as to modify the implementation of a feature, there are 2 implementations of a and b,a is a huge change, that is, suitable for the new write a class to implement, B is a small change, you can directly modify the internal structure of the class. If you use a scheme, and the system has multiple references, and even need to pay attention to the changes in the constructor and its parameters, it means that multiple modifications, which violate the try principle, is a burden on development. If the B scheme is used, it is important to pay attention to whether the internal implementation changes will affect the external environment and whether there is any place to make the modification not closed. Testability does not have to be said, because it is directly new, so can not be moq, resulting in a perfect unit test.
When directly using the factory model, we need to do the abstraction, that is, the function code into the abstract and implementation of the two parts, the function caller calls the method provided by the factory to obtain specific implementation objects, which ensures that the system is interface-oriented programming, to help programmers write high-cohesion low-coupling logic. It also raises some problems, such as the possibility that more factory classes need to be maintained, and there is a coupling between the function caller and the factory class, and if the abstract Factory mode is used to decouple, the content of the configuration file needs to be increased.
The IOC container makes it very easy to define the acquisition strategy for an implementation object that conforms to an interface, to configure and manage the life cycle of an object without having to write and maintain factory code, with the best maintainability and testability.
Overview of the IOC