Dependency injection

A diagram of an archetypical dependency injection container for the .NET platform.
Dependency injection is often used alongside specialized frameworks, known as 'containers', to facilitate program composition.
PetManager gets injected into PetController and PetRepository gets injected into PetManager

In software engineering, dependency injection is a programming technique in which an object or function receives other objects or functions that it requires, as opposed to creating them internally. Dependency injection aims to separate the concerns of constructing objects and using them, leading to loosely coupled programs.[1][2][3] The pattern ensures that an object or function that wants to use a given service should not have to know how to construct those services. Instead, the receiving "client" (object or function) is provided with its dependencies by external code (an "injector"), which it is not aware of.[4] Dependency injection makes implicit dependencies explicit and helps solve the following problems:[5]

  • How can a class be independent from the creation of the objects it depends on?
  • How can an application, and the objects it uses support different configurations?

Dependency injection is often used to keep code in-line with the dependency inversion principle.[6][7]

In statically typed languages using dependency injection means that a client only needs to declare the interfaces of the services it uses, rather than their concrete implementations, making it easier to change which services are used at runtime without recompiling.

Application frameworks often combine dependency injection with inversion of control. Under inversion of control, the framework first constructs an object (such as a controller), and then passes control flow to it. With dependency injection, the framework also instantiates the dependencies declared by the application object (often in the constructor method's parameters), and passes the dependencies into the object.[8]

Dependency injection implements the idea of "inverting control over the implementations of dependencies", which is why certain Java frameworks generically name the concept "inversion of control" (not to be confused with inversion of control flow).[9]

  1. ^ Seemann, Mark. "Dependency Injection is Loose Coupling". blog.ploeh.dk. Retrieved 2015-07-28.
  2. ^ Cite error: The named reference MarkSeeman2011P4 was invoked but never defined (see the help page).
  3. ^ Niko Schwarz, Mircea Lungu, Oscar Nierstrasz, “Seuss: Decoupling responsibilities from static methods for fine-grained configurability”, Journal of Object Technology, volume 11, no. 1 (April 2012), pp. 3:1–23.
  4. ^ "HollywoodPrinciple". c2.com. Retrieved 2015-07-19.
  5. ^ "The Dependency Injection design pattern – Problem, Solution, and Applicability". w3sDesign.com. Retrieved 2017-08-12.
  6. ^ Erez, Guy (2022-03-09). "Dependency Inversion vs. Dependency Injection". Medium. Retrieved 2022-12-06.
  7. ^ Mathews, Sasha (2021-03-25). "You are Simply Injecting a Dependency, Thinking that You are Following the Dependency Inversion…". Medium. Retrieved 2022-12-06.
  8. ^ "Spring IoC Container". Retrieved 2023-05-23.
  9. ^ Fowler, Martin. "Inversion of Control Containers and the Dependency Injection pattern". MartinFowler.com. Retrieved 4 June 2023.