What is dependency injection?

Dependency injection is a software technique to make creating composite objects easier. A provider object has the dependencies another object needs in order to operate.  Dependency injection frees developers from having to do two main things.  One helps free them from having to manually create objects.  Secondly, it also frees them up from having to lug individual dependencies around in a code base to create said objects.

Dependency injection libraries free devs from having to construct an object.  Finally, all they need to do is define what the object depends on. Also, where they need it.

Let’s start with an overly simplistic example. Imagine you have a smart suitcase that you can store things in.  Smart suitcases can make composite objects (objects built on other objects it contains).  Let’s say that you decide to store ice cream, ice cream cones, sprinkles, whip cream, chocolate sauce, and bananas.  Since it is smart it can now make Banana Splits (since it has bananas and ice cream).  The smart suitcase can also make sundaes (because it has ice cream, whip cream, and chocolate sauce, can’t forget that).  As well, it can also make an ice cream cone.  It frees you from having to cary the items individually around and can make them for you.

You can read more about dependency injection on <a href=”https://en.wikipedia.org/wiki/Dependency_injection>wikipedia</a>.

If you are an Android developer you’ve probably heard of <a href=”https://github.com/google/dagger”>Dagger</a>.  Dagger is a dependency injection library is popular with Android developers.  It’s popular with Android Developers due to the architecture of the Android platform itself. One or more objects represent a screen on Android.  As you can imagine an app may have lots of screens.  Often times these screen objects require the same dependencies.  Dependency injection libraries makes initializing these screens easier.

Leave a Reply

Your email address will not be published. Required fields are marked *