What's Dependency Injection? - fanglinliu/fanglinliu.github.io GitHub Wiki

http://stackoverflow.com/questions/130794/what-is-dependency-injection http://martinfowler.com/articles/injection.html

up vote 937 down vote accepted Basically, instead of having your objects creating a dependency or asking a factory object to make one for them, you pass the needed dependencies in to the constructor or via property setters, and you make it somebody else's problem (an object further up the dependency graph, or a dependency injector that builds the dependency graph). A dependency as I'm using it here is any other object the current object needs to hold a reference to.

One of the major advantages of dependency injection is that it can make testing lots easier. Suppose you have an object which in its constructor does something like:

public SomeClass() { myObject = Factory.getObject(); } This can be troublesome when all you want to do is run some unit tests on SomeClass, especially if myObject is something that does complex disk or network access. So now you're looking at mocking myObject but also somehow intercepting the factory call. Hard. Instead, pass the object in as an argument to the constructor. Now you've moved the problem elsewhere, but testing can become lots easier. Just make a dummy myObject and pass that in. The constructor would now look a bit like:

public SomeClass (MyClass myObject) { this.myObject = myObject; } Most people can probably work out the other problems that might arise when not using dependency injection while testing (like classes that do too much work in their constructors etc.) Most of this is stuff I picked up on the Google Testing Blog, to be perfectly honest...

shareimprove this answer edited Aug 8 '13 at 15:16 answered Sep 25 '08 at 0:49

wds 15.7k83472 12 Acknowledging that Ben Hoffstein's referenceto Martin Fowler's article is necessary as pointing a 'must-read' on the subject, I'm accepting wds' answer because it actually answers the question here on SO. – AR. Sep 26 '08 at 16:55 44 +1 for explanation and motivation: making the creation of objects on which a class depends someone else's problem. Another way to say it is that DI makes classes more cohesive (they have fewer responsibilities). – Fuhrmanator Nov 29 '12 at 18:26 7 You say the dependency is passed "in to the constructor" but as I understand it this isn't strictly true. It's still dependency injection if the dependency is set as a property after the object has been instantiated, correct? – Mike Vella Aug 7 '13 at 19:52 1 @MikeVella Yes, that is correct. It makes no real difference in most cases, though properties are generally a bit more flexible. I will edit the text slightly to point that out. – wds Aug 8 '13 at 15:14 4 Given the relative simplicity of the DI pattern, this concise answer is far more clear than Fowler's article which I just read. The references to testing and network access certainly help; they're the reason I came looking for an explanation of dependency injection! – Matt Oct 3 '13 at 15:07 show 1 more comment