Dependency Injection as Inversion of Control¶
Dependencies should be provided to objects rather than created by them, because creation inside a module couples it to specific implementations and prevents substitution.
Tension Addressed¶
Flexibility vs. Simplicity
Recognizable As¶
Constructor parameters for collaborators; factory methods for complex creation; configuration at application boundaries; test doubles injected in tests
Canonical Sources¶
Fowler, "Inversion of Control Containers and the Dependency Injection pattern" (2004); Martin, "Clean Code" (2008)
Transfers To¶
Testing (mock injection), Software Architecture (plugin systems), Functional Programming (function parameters as dependencies)
The deeper principle is inversion of control: instead of modules controlling their dependencies, control flows inward from application boundaries. This inverts the call graph for dependency resolution without changing runtime behavior.
Anti-pattern: Service Locator provides dependencies through global lookup, trading explicit dependencies for hidden coupling to the locator.
Related Concepts¶
- Coupling - Software Architecture