DI — Dependency Injection

Oliver Jumpertz
3 min readJan 23, 2022

Dependency injection is a technique in which an object receives other objects it depends on.

It is actually one form of the broader technique inversion of control.

What is it?

Dependency injection is a technique where every other object (service) an object (client) depends on is injected from the outside. So instead of simply instantiating everything on its own, a class relies on it being passed in from the outside.

You can also view it this way: Imagine your favorite RPG characters could only wear the equipment they started with. No way to change the equipment or the look of your characters’ equipment from the outside.

Would be boring and inflexible, wouldn’t it?

This is why modern RPGs let you change your character’s equipment. And you can compare this to DI. Every slot requires a certain type of equipment. And you can change your character’s equipment on the fly, by dropping a new piece into a fitting slot.

The analogy/example is of course one of a mutable object, but I want you to get the general idea of what it is. And it would be pretty bad if you had to kill your…

--

--