- Classes are more modular, as they depend only on the interface of passed-in dependencies. Class behavior can be changed by swapping out a new component.
- Testing is simplified, since stubs can be substituted for any dependency.
- It's harder to understand how a class works when reading just that class. You may have to track down its invocation to see what kind of components are passed in.
- Is DepedencyClass.expects(:new).returns(stub)) a smell, since we should have injected that stub to the class that uses it instead?
- If a class uses DI, should one only pass in already-instantiated dependencies, or is it okay to let the calling class instantiate them?
- Am I missing any pros or cons?
@r00k
I think what you're pointing against DI is a "problem" with duck-typing in general. You never know if some parameter you received is exactly what you where expecting, and the only way to "see what's happening" is checking the calling code and/or searching for the instantiation of such object.
The inappropriate quotes surrounding "problem" are due to the fact that it's not really a problem, just a trade-off made by the language designers.