As a heavy user of dependency injection, I’m always careful when applying it. “If you have a hammer, everything looks like a nail.” Nikola Malovic mentions on his blog the following laws about the usage of an IOC container.
Nikola’s 5 laws of IoC:
- “Store in IoC container only services. Do not store any entities.”
- "Any class having more then 3 dependencies should be questioned for SRP violation"
- “Every dependency of the class has to be presented in a transparent manner in a class constructor.”
- “Every constructor of a class being resolved should not have any implementation other then accepting a set of its own dependencies.”
- “IoC container should be explicitly used only in Bootstrapper. Any other “IoC enabled” code (including the unit tests) should be completely agnostic about the existence of IoC container."
Although I don’t agree with all of his laws, he makes some good points.