Real world solutions using Dependency Injection

c#, dependency-injection, java, spring

Solution

Just the other day, I decided to read up on dependency injection. Until then, I only knew the word. Honestly, my reaction to Martin Fowler's article was, "That's it?"

I have to agree with James Shore:

"Dependency Injection" is a 25-dollar term for a 5-cent concept.

That doesn't mean at all that it's a bad concept. But seriously, when an instance `A` needs to work with another instance `B`, it comes down to these choices:

let `A` find `B`:

That means `B` must be global. Evil.

let `A` create `B`:

Fine, if only `A` needs `B`. As soon as `C` also needs `B`, replace `A` by `C` in this list. Note that a test case would be a `C`, so if you do want to test, this choice is gone as well.

give `B` to `A`:

That's dependency injection.

Am I missing something? (Note that I'm from the Python world, so maybe there are language specific points I'm not seeing.)

Problem

I was reading about DI thoroughly, and it seems interesting. So far, I'm totally living without it. All the examples i saw are related to JNDI and how DI helps you being more flexible. What is real life applications/problems that you solved with DI that will be hard to solve in other ways? UPDATE All the answers till now are educating, but to rephrase the question, I'm looking for examples in your programming life, that made you say "this problem will be best solved with a DI framework".

Original source