Dependency Injection is a really interesting and useful pattern, but it's often misunderstood. In this chapter, we tried to present every aspect of Dependency Injection, debunking its myths and falsehoods, and showing when and how to use it.
We started the exploration of Dependency Injection with its definition, from Martin Fowler's seminal article, and from several other influential sources. A pattern is useless without the context in which it must be applied. We identified two major scenarios where DI is useful: Separation of concerns, to help the development of loosely-coupled components and hence create more flexible and maintainable code, and Testing, where problematic collaborators could be replaced by Test Double, a simplified and controlled version of the dependencies. There are different ways of implementing DI, such as injecting in the constructor...