Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Real world solutions using Dependency Injection

Tags:

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".

like image 308
medopal Avatar asked Jan 21 '10 06:01

medopal


People also ask

What is dependency injection with real time example?

Dependency Injection can exist between two objects, for instance, a flashlight and a battery. The flashlight needs the battery to function. However, any changes made to the battery, such as switching it with another brand/set of batteries, does not mean the dependent object (flashlight) also needs to be changed.

Which problem can you solve using dependency injection?

Dependency injection supports these goals by decoupling the creation of the usage of an object. That enables you to replace dependencies without changing the class that uses them. It also reduces the risk that you have to change a class just because one of its dependencies changed.

Can you give few examples of dependency injection?

Two popular dependency injection frameworks are Spring and Google Guice. The usage of the Spring framework for dependency injection is described in Dependency Injection with the Spring Framework - Tutorial. Also Eclipse RCP is using dependency injection.

Where do we use dependency injection?

More specifically, dependency injection is effective in these situations: You need to inject configuration data into one or more components. You need to inject the same dependency into multiple components. You need to inject different implementations of the same dependency.


2 Answers

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:

  1. let A find B:

    That means B must be global. Evil.

  2. 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.

  3. 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.)

like image 106
balpha Avatar answered Oct 23 '22 08:10

balpha


Yesterday I found a mobile on the bus. The person who lost it had no clue about the person possessing her mobile. I called her dad and told him I have the mobile of his daughter. So I injected the dependency from me into him. Typically a case of the hollywood principle "Don't call us (because you can't!), we call you". Later he came and picked up his daughters phone.

I'd call that a real world problem which I solved by dependency injection, isn't it?

In my opinion, DI is not THE way to solve problems, for which we would not have another solution. Factories can be another way to solve such problems. So there is no real answer for your question, because DI is just one way besides others. It is just a pretty hip, although very elegant way.

I really enjoyed DI when I had this DAOs, which needed an SQLMapper. I just had to inject the different mappers into the fatherclass once and the rest was done by configuration. Saved me a lot of time and LOCs, but I still can't name this a problem for which there is no other solution.

like image 28
bl4ckb0l7 Avatar answered Oct 23 '22 08:10

bl4ckb0l7