An application is assembled from objects, and somebody has to wire them together: the order service needs a database, the mail sender needs a mail server. Dependency injection moves this work from inside the classes outwards: a class declares what it needs, and the assembling code supplies the implementations. The class no longer knows where to get a database — it can be assembled differently.
What it looks like
A dependency is an object a class works with but does not create itself: a repository, an API client, a mail sender, configuration. The class receives them through constructor parameters or properties, while the concrete implementations are chosen by external code — by hand or through a DI container.
class OrderService: def __init__(self, repo, mailer): self.repo = repo # the dependency came from outside self.mailer = mailer # wiring is done by external code, not by the class itselfservice = OrderService(PostgresRepository(db_url), SmtpMailer(smtp_host)) # in tests the same parameters accept test doublesservice = OrderService(FakeRepository(), FakeMailer())For comparison: if a class writes self.repo = PostgresRepository(db_url) itself, changing the database means editing the class. Injection turns the direction around: the class describes what it needs, and the choice of implementation is made outside. Fowler put the general principle this way: separate configuration from use.
Ways of passing dependencies and containers
| Technique | How the dependency is passed |
|---|---|
| Constructor injection | the dependencies are mandatory constructor parameters: the object cannot be built without them |
| Setter injection | the dependency is assigned by a method or a property after the object has been created |
| Injection through an interface | the class implements a method such as setMailer, through which the assembler passes the dependency |
Spring describes DI as follows: an object declares its dependencies through constructor arguments, factory method arguments or properties, and the container supplies them when creating the object. A DI container automates this wiring: from configuration it works out which implementation sits behind each interface, builds the object graph and manages the objects’ life cycle — from a single instance for the whole application to a new one per request. Built-in containers exist in Spring, .NET and Angular; Python, Go and Flutter have separate libraries.
Why it matters
- testing: instead of a real database or mail server, the object is given a test double, so a unit test needs neither the network nor data;
- swapping implementations without editing the class: the same assembly can be pointed at PostgreSQL or at a stub;
- explicit dependencies: the constructor parameters show what the class depends on;
- life cycle management: the container decides who owns a shared object and when to release it.
Where the term comes from
The name was established by Martin Fowler in an article of 23 January 2004, after a discussion with IoC advocates. The lightweight Java containers of the time called what they did “inversion of control” (IoC), but Fowler found that name too general: inversion of control also describes the behaviour of any framework that calls the application’s code itself, such as the user interface event loop. For the specific case — when a separate assembler object supplies implementations of the dependencies instead of the class looking them up itself — the name “dependency injection” was chosen.
Sources
the term is written from these pages- Martin Fowler — Inversion of Control Containers and the Dependency Injection patternmartinfowler.com/articles/injection.html(opens in a new tab)
- Spring Framework Reference — Dependencies and DI in the IoC containerdocs.spring.io/spring-framework/reference/core/beans/dependencies/factory-collaborators.html(opens in a new tab)
- Microsoft Learn — .NET dependency injectionlearn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection(opens in a new tab)
Verified 8 Nov