juniorDependency Injection
What is Dependency Injection (DI)?
Updated Apr 28, 2026
Short answer
DI is a design pattern used to implement Inversion of Control (IoC), allowing the creation of dependent objects outside of a class and passing them in.
Deep explanation
Instead of a class instantiating its dependencies using the new keyword, dependencies are provided (injected) to the class, usually via its constructor. This removes hard-coded dependencies, making the class easier to test, maintain, and decouple from specific implementations.
Real-world example
A Car class needing an Engine. Instead of the Car building its own V8Engine, the factory passes an IEngine into the Car.
Common mistakes
- Confusing DI with a DI Container. DI is the pattern
- a container is a tool that automates it.
Follow-up questions
- What is Inversion of Control (IoC)?