Glossary termDesign technique

Dependency Injection

Also known as: DI

Technique in which an object receives the components it depends on from outside rather than creating them, making code easier to test and change.

Dependency injection is a way of supplying a piece of code with the other components it needs, its dependencies, instead of having it construct them internally. Suppose an order-processing class needs to send emails. Without injection, it might create a specific email client inside its own code, tying it permanently to that client. With injection, the email sender is passed in, typically through the constructor, so the class works with any object that offers the required interface. The class declares what it needs, and something else decides what to provide.

The technique is a form of inversion of control, in which responsibility for wiring components together moves from the components themselves to the code that assembles the application. Martin Fowler popularized the name in a 2004 article on inversion of control containers, distinguishing constructor, setter and interface injection. Many frameworks, including Spring for Java, Angular and ASP.NET Core, provide containers that create objects and inject their dependencies automatically based on configuration or type information. In smaller programs, passing dependencies by hand is often enough.

The main benefit is testability: a class that receives its database or network client from outside can be given a fake version in unit tests, so tests run quickly and without real infrastructure. Injection also reduces coupling, letting implementations be swapped without editing the code that uses them, in line with the principle of abstraction. Critics note that heavy use of automated containers can make it hard to see where objects come from, turning simple programs into webs of configuration. Like other design patterns, it pays off most in large, long-lived codebases.

Key points

  • With dependency injection, components receive their collaborators from outside instead of constructing them.
  • Martin Fowler popularized the name in a 2004 article on inversion of control containers.
  • Injecting dependencies lets tests substitute fakes for databases, networks and other slow components.
  • Framework containers can automate the wiring, but heavy use may obscure how a program is assembled.

Related entries

Glossary term

Design Pattern

Programming

Reusable, named solution to a commonly recurring problem in software design, described as a template rather than as finished code.

Glossary term

Unit Testing

Programming

Practice of verifying that small, individual pieces of code, such as single functions or classes, behave correctly when tested in isolation.

Glossary term

Reusable software platform that provides a predefined structure and calls developer-written code at set points, shaping how an application is built.

Person

Martin Fowler

Programming

British software developer and author known for popularizing refactoring, enterprise design patterns, microservices and agile development practices.

Glossary term

Abstraction

Programming

Principle of managing complexity by hiding details behind simpler interfaces, so a component can be used without knowing how it works inside.

Glossary term

Programming paradigm that organizes software around objects, which bundle data with the methods that operate on it and interact by sending messages.

Glossary term

Software development practice in which a failing automated test is written before the code that makes it pass, after which the code is refined.