What is Dependency Injection
Pattern for passing dependencies externally
Dependency Injection (DI) is a design pattern where an object receives its dependencies from external sources rather than creating them itself.
Types of Injection
- Constructor Injection — via constructor
- Setter Injection — via setter methods
- Interface Injection — via interface
- Property Injection — via public properties
Benefits
- Loose Coupling — components are easily replaceable
- Testability — easy to substitute mocks and stubs
- Flexibility — behavior is configured externally
- Reusability — more modular code
- Clean Architecture — dependency inversion
Popular DI Containers
- Spring (Java)
- .NET Core DI (C#)
- InversifyJS (TypeScript)
- Dagger (Android)
- Angular DI