Image Image Image Image Image Image Image Image Image Image

Mudassir Shahzad | June 23, 2017

Scroll to top

Top

No Comments

Dependency Injection Explained (Especially for beginners)

Dependency Injection Explained (Especially for beginners)
Mudassir Shahzad

Somebody asked a question : How to explain dependency injection to a 5-year old?

Well, sounds like that kid’s in for a hard life…
I suggest, start with “Once upon a time” 😛

Jokes apart, This question is constructive. A 5 yr old thinks differently than an adult, explaining complex concepts to a kid is a skill and there is nothing argumentative about it.

 

So, I give you dependency injection for five year olds (come on, not literally)

“When you go and get things out of the refrigerator for yourself, you can cause problems. You might leave the door open, you might get something Mommy or Daddy doesn’t want you to have. You might even be looking for something we don’t even have or which has expired.

What you should be doing is stating a need, “I need something to drink with lunch,” and then we will make sure you have something when you sit down to eat.”

 

When writing a class, it’s natural for it to make use of other objects.

For example,

If you have a class Employee and this employee has an Address you can have the Employee class defined as follows:

 

These other objects (or services) are dependencies.
The simplest way to write the code is simply to create and use those other objects.

Like,

 

But this means your object has an inflexible relationship to those dependencies: no matter why you are invoking your object, it uses the same dependencies.

 

In this example, no matter why you are creating the employee object, you have to have an address object to pass to its constructor.

 

Employee class depends on someAddress, and they’re highly coupled. On the other hand, let’s say I have a method InjectSomeAddress:

 

 

This is useful during testing. With normal operation you can use heavy, database-using, network-using classes etc. while for tests passing a lightweight, mock implementation. With tightly coupled code you can’t do that.

 

It is sometimes easy to take this too far. At some point you have to make things concrete, or your program will be less readable and understandable. So use this technique mainly at components boundary, and know what you are doing. Make sure you are taking advantage of loose coupling. If not, you probably don’t need it in that place. DI may make your program more complex. Make sure you make a good tradeoff. In other words, maintain good balance. As always when designing systems.

 

Dependency Injection enables you to create your object and provide it with dependencies to use. So you might create a database connection (or an address object, in this case) to use, then hand it to your object. This way, you can create your object with different dependencies at different times, making your object more flexible.

 

This is dependency injection, where you “inject” the dependencies into the object.

 

BTW: In the modern presentation style of using flickr photos to illustrate concepts, this could be illustrated with an addict shooting themselves up with drugs. Oh, wait, that’s injection dependency… OK, sorry, bad joke.

 

Some good frameworks that provide dependency injection in java:
JSF (yes, it does), Spring, Jboss Seam

Submit a Comment

not found