14 July 2014
It seems that over the years, many people have accepted that Dependency Injection (DI), which is sometimes called Inversion of Control (IoC), is a "good thing" and that they should be "doing it." But, few of those that I talk to seem to know why that is. And, fewer still can tell you the difference between the Factory Pattern and DI. This is usually an indicator of how they (mis)utilize their DI framework. Many of the implementations that I have seen use it as a glorified factory. But, DI is a special implementation of the factory pattern that can do so many more things to help improve the structure of your code and improve testability than just serving out objects. The relationship between the factory pattern and DI is like the relationship between rectangles and squares. All squares are rectangles, but a rectangle is not necessarily a square. Just like all DI frameworks follow the factory pattern, but a factory is not necessarily a DI container.