Dependency Injection
In depthConcept
Also known as: DI
Design pattern where dependencies are handed in from outside instead of created by the object itself.
Dependency Injection, explained
Written by EduVerseWhat it is
Dependency injection is a design pattern where a class receives the objects it needs, usually through its constructor, instead of creating them itself. An OrderService doesn’t call new EmailSender(); it asks for an EmailSender, and something else, often a framework, supplies one.
Why teams use it
When a class builds its own dependencies, it’s tied to those exact implementations and hard to test. With injection you can pass a fake email sender into a unit test, or swap in a different implementation without touching the class. Frameworks like Spring and ASP.NET Core create these objects and wire them together for you.
An example from work
You add a PaymentClient parameter to the constructor of a Spring service, and the app refuses to start: the constructor required a bean of type PaymentClient that could not be found. Spring tried to inject it, but no class is registered for that type. Adding @Component or @Service to the implementation, or writing a @Bean method, fixes it.
Our own explanation, not a quote from the book.
Where it fits
Loose coupling, testability, modular design
Coverage in the book
Covered in depth: multiple pages with explanations, examples and simulations.