What is Inversion of Control?

前端 未结 30 2887
清歌不尽
清歌不尽 2020-11-22 00:13

Inversion of Control (IoC) can be quite confusing when it is first encountered.

  1. What is it?
  2. Which problem does it solve?
  3. When is it appropria
30条回答
  •  自闭症患者
    2020-11-22 00:41

    1. Inversion of control is a pattern used for decoupling components and layers in the system. The pattern is implemented through injecting dependencies into a component when it is constructed. These dependences are usually provided as interfaces for further decoupling and to support testability. IoC / DI containers such as Castle Windsor, Unity are tools (libraries) which can be used for providing IoC. These tools provide extended features above and beyond simple dependency management, including lifetime, AOP / Interception, policy, etc.

    2. a. Alleviates a component from being responsible for managing it's dependencies.
      b. Provides the ability to swap dependency implementations in different environments.
      c. Allows a component be tested through mocking of dependencies.
      d. Provides a mechanism for sharing resources throughout an application.

    3. a. Critical when doing test-driven development. Without IoC it can be difficult to test, because the components under test are highly coupled to the rest of the system.
      b. Critical when developing modular systems. A modular system is a system whose components can be replaced without requiring recompilation.
      c. Critical if there are many cross-cutting concerns which need to addressed, partilarly in an enterprise application.

提交回复
热议问题