Section: IT & Technology · Software DevelopmentDifficulty: Medium

Dependency Injection

USUK

A design pattern where objects receive their dependencies from external sources rather than creating them.

Also: DI · IoC

Definition

Dependency injection (DI) is a software design pattern where a class receives its dependencies (services or objects it needs) from external sources rather than instantiating them itself. This inversion of control decouples components, making code more testable (dependencies can be mocked), flexible (implementations can be swapped), and maintainable. DI frameworks like Spring (Java), Angular, and .NET's built-in DI container automate the wiring of dependencies throughout application code.

Example

Instead of a UserService creating its own DatabaseConnection, it declares the connection as a constructor parameter; the DI framework injects the appropriate connection at runtime, enabling easy substitution with a mock during unit tests.

Synonyms

  • inversion of control
  • DI
  • constructor injection
  • IoC

Antonyms / Opposites

  • hard-coded dependencies
  • tightly coupled code

Images

CC-licensed · free to use
More on Wikimedia
Loading images…

Video

  • solid-principles
  • unit-testing
  • design-pattern
  • object-oriented-programming

Dictionary Entry

Back to IT & Technology