</>CodexBash
All articles
Programming

Dependency injection

Dependency injection explained from first principles: the problem with creating dependencies inside classes, constructor, setter and method injection, programming to interfaces, wiring by hand with a composition root, DI containers in Spring, scopes, testing with fakes and mocks, DI in Python and TypeScript, and common pitfalls.

CodexBash, , 6 min read

Dependency injection (DI) is one of those terms that sounds far more complicated than the idea behind it. Put simply: instead of a class creating the things it needs, those things are given to it from outside. That small change makes code easier to test, easier to change and easier to understand, and it is the foundation of frameworks like Spring, Angular and NestJS. This article explains the idea from first principles, shows DI without any framework, then explains what containers like Spring add.

Sign in to read the rest

The whole 6 minute article is free with an account, along with every problem, lesson and tutorial. No card.