Software engineering / Dependency inversion and injection

Dependency inversion and injection

A signup class needs to send email. We watch it build its own mailer, see why that hurts, then turn the dependency around and hand the mailer in from outside.

Intermediate7 steps~7 min
Signup — the class that needs a mailer
Mailer — the abstraction: anything with send()
SmtpMailer — a concrete class: sends real email
FakeMailer — a stand-in for tests: only records
// THE SHORT VERSION

In 30 seconds

A dependency is another object your code needs to do its job. If a class creates that object itself, it is stuck with one exact implementation.

Dependency inversion says: depend on an abstraction, a description of what you need, instead of a concrete class. Dependency injection is how the real object arrives: it is passed in from outside, usually through the constructor.

The pay-off is that parts can be swapped. A test can pass in a fake, and a DI container can do the passing for you in large programs.

Dependency inversion and injection, step by step

STEPS01 / 07

The code is JavaScript, which has no interface keyword: “Mailer” is an agreement that the object has send(to, text). Typed languages write it down as an interface. Containers differ in detail.

STEP 01 / 07 A hard-wired dependency
THE MATH
Signup needs a mailer this.mailer = new SmtpMailer()
  1. Step 1: A hard-wired dependency. A Signup class registers new users and sends each one a welcome email. To send mail it needs a mailer: that is its dependency. Here Signup creates the mailer itself, naming the exact class, SmtpMailer, which talks to a real mail server. The two are now welded together.
  2. Step 2: Why that hurts. Two problems follow. A test that calls register() really sends an email on every run, which is slow, needs a network, and lands in real inboxes. And if the team wants a different way to send mail, the only way to switch is to open Signup and edit it. Signup cannot be used, or tested, without that one mailer.
  3. Step 3: Depend on an abstraction. Describe what Signup needs as a shape, not a class: a Mailer is anything with send(to, text). Signup now refers only to that abstraction, and SmtpMailer is one thing that fits it. The arrow that pointed from Signup down to the detail is gone; the detail now points up at the abstraction. That is dependency inversion.
  4. Step 4: Inject via the constructor. Signup still needs a real object at run time, but it must not create one. So it asks for one: the constructor takes a mailer as a parameter and stores it. Code outside, here the program’s main, builds the SmtpMailer and passes it in. Handing an object its dependencies from outside is dependency injection.
  5. Step 5: Swap in a fake for tests. Because Signup accepts any Mailer, a test can inject a FakeMailer whose send() only adds the message to a list. No network is touched, the test runs in milliseconds, and it can check exactly what Signup tried to send: one message, to ada@example.com. Signup itself was not changed to make this possible.
  6. Step 6: Wire it up in one place. Someone still has to choose the real classes. That happens once, at start-up, in one place often called the composition root. It builds the objects with no dependencies first, then passes them to the objects that need them. Signup here needs two things, a store for users and a mailer, and is built last.
  7. Step 7: A container does the wiring. In a large program the wiring code grows long. A DI container automates it: you register which class to use for each abstraction, ask for a Signup, and the container builds what the constructor needs and passes it in. It saves typing, but the wiring is less visible, and a missing registration often shows up only at run time.
VALUES SO FAR
Signupnew SmtpMailerhardwired
tests····problem
Mailer····abstract
arrows····inverted
inject····technique
in tests····swapped
sent····checked
wiring····root
auto····optional

Spotted a mistake on this page?