NEW
Also known as: DI
A design pattern supplying an object’s dependencies from external sources to improve modularity, testability, and decoupling.
DESCRIPTION
Dependency Injection externalizes object creation and wiring, letting components declare interfaces and receive implementations at runtime or build time. DI supports loose coupling, easier testing with mocks, and cleaner configuration. Frameworks provide containers for lifecycle management and resolution. DI contrasts with service locator by avoiding hidden global dependencies. Proper design avoids over-injection, cyclic graphs, and runtime misconfiguration. It is a core practice in modern software frameworks.
ALTERNATIVE DEFINITIONS
Martin Fowler
Dependency Injection is a specific form of Inversion of Control where dependencies are provided to a component rather than the component locating them itself.
Clarifies DI as a subset of IoC with provided dependencies.
Spring Framework
The Spring IoC container manages the dependencies of your application objects by injecting them at runtime.
Framework-centric definition emphasizing container-managed injection.
Microsoft Learn
Dependency injection is a technique for achieving loose coupling between objects and their collaborators.
Highlights loose coupling benefits in .NET.
RELATED TERMS
Inversion of Control Loose Coupling Interface Unit Testing Containers Spring Framework .NET DI Angular DI Configuration
COMPETING TERMS
Service Locator
KNOWLEDGE AREAS
Software Engineering Platform & Tooling Infrastructure
TARGET AUDIENCE
Software Developer Software Architect DevOps Engineer Full Stack Developer Solution Architect
EXTERNAL SOURCES
- Wikipedia - Dependency injection
- Martin Fowler - Seminal article on DI
- Spring - Spring IoC and DI reference
- Microsoft - .NET DI documentation
TOP QUESTIONS
Why use Dependency Injection?
DI reduces coupling, improves testability, and simplifies configuration; components depend on interfaces, not concrete implementations.
Category: Relevancy
What is the difference between DI and Service Locator?
DI provides dependencies explicitly; Service Locator hides them behind a global registry, making dependencies less transparent.
Category: Misunderstanding
What are common DI pitfalls?
Overly complex graphs, runtime configuration errors, and circular dependencies; keep interfaces small and validate wiring.
Category: Mistakes
Which frameworks support DI?
Spring, .NET Core, Angular, NestJS, and Guice provide DI containers and patterns.
Category: Overview
How does DI improve testing?
You can inject mocks or stubs, isolating components and enabling fast unit tests.
Category: Testing
How should lifecycles be managed in DI?
Choose singleton, scoped, or transient lifetimes based on resource usage and thread safety.
Category: Best practices
Can DI be used in microservices?
Yes, DI helps manage service dependencies and configuration per microservice, improving modularity.
Category: Opinion
What’s a hot topic around DI?
Compile-time DI and code generation to reduce runtime overhead and improve startup.
Category: Topic
How to document dependencies with DI?
Use diagrams, configuration manifests, and container modules to make wiring explicit and reviewable.
Category: Management