Bookmarks

DI — dependency injection

A design technique in which an object does not create the dependencies it needs itself, but receives them from outside — usually through constructor parameters.

Also: dependency injection pattern, injecting dependencies

checked 8 Nov

All terms

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.

pythonDependencies are declared as constructor parameters and supplied at assembly time
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

TechniqueHow the dependency is passed
Constructor injectionthe dependencies are mandatory constructor parameters: the object cannot be built without them
Setter injectionthe dependency is assigned by a method or a property after the object has been created
Injection through an interfacethe 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

Verified 8 Nov

Not to be confused with

what it is mixed up with and how it differs

  • SQLiSQL injection

    SQL injection is an attack in which someone else’s code is slipped into a database query; dependency injection is an assembly technique where the needed objects are supplied by external code.

In depth in the references

where the topic is covered in detail