Skip to catalogue

56

dependency injection

also DI

Pass collaborators in. Do not let a function new up the world.

What is dependency injection?

Dependency injection means a unit receives the things it uses — a clock, a database, a mailer — instead of constructing them. Tests can pass fakes. Production passes the real ones.

Why does dependency injection matter when vibe coding?

Models hide `new Stripe()` and `fetch` inside business code. You cannot test the rule without the network. Name DI or every “unit test” becomes an integration accident.

How do you do dependency injection?

Constructor or function argument for anything that talks to time, disk, or the network. One composition root wires the real ones. Tests pass fakes.

How do you ask a model for dependency injection?

Inject the (clock / db / mailer). Do not construct it inside the function. Tests must pass a fake. Wire the real one in one place.

What goes wrong with dependency injection?

A DI container for three classes. You wanted a parameter, not a framework.

adjacent