Dagger 2 for Android Beginners — DI part II

Hari Vignesh Jayapalan
4 min readDec 1, 2017

--

This story is the third part of the series, Dagger 2 for Android Beginners. If you did not read the previous one, you can start from here.

Series Pitstops

Previously on Dagger 2..

In the previous pit stop, we had our discussion deeper on dependencies and dependency injection. We also saw an example — the battle of bastards, where we created the class War with hard dependency.

Note: If you’re not comfortable with the way of my storytelling — keeping Game of Thrones as the concept, feel free to change the name of the classes when you proceed.

Using The Dragon Glass

Let’s recap our previous example — the battle of bastards. The class War had two dependencies: Boltons and Starks.

Now it’s time to use dragon glass to kill the White Walkers. Yes, we’ll use the dependency injection concept to eliminate the hard dependency.

To recap on dependency injection, the class should not create or have its dependencies on it. Instead, it needs to get from outside. So let’s bring the dependencies Starks and Boltons from outside to the Class War.

The above example is a form of constructor DI. I know most you would think that this I have done it already in my projects. Yes, you are right. Many of us use the DI concept without knowing it.

Also, the Class War, should not only know how to perform a certain task but also where to find the collaborators it needs to accomplish it. If instead, we provide to our object everything it needs to perform its job, then the previous problems disappear. The class can easily work with different collaborators and can be tested in isolation. In an application that relies on dependency injection, the objects never have to hunt around for dependencies or construct them themselves. All the dependencies are provided to them or injected into them so that they are ready to be used.

At some point, of course, someone has to instantiate the dependencies and actually provide them to the objects that might require them. This phase is usually done at the entry point of the application. On the normal Java application, for example, this code would be found inside the main() method, as shown below. On Android, it could be done inside an Activity’s onCreate() method.

Here’s the class BattleOfBastards, where we create the dependencies Starks and Boltons and inject them via constructors to the class War — as War is dependent on the dependencies Starks and Boltons.

Now, it’s time to appreciate yourself and celebrate — yes! we killed the hard dependency a.k.a the White Walkers. Hope you understood the concept of what we’re trying to achieve.

Refer the following branch for the complete code. The project is written in IntelliJ — just a normal Java project.

TL;DR

On this pitstop, we understood that the class should not create or have its dependencies on it. Instead, it needs to get it from outside.

We also saw the plan of simple dependency injection in action. We took the example of battle of bastards and tried to eliminate the hard dependency on the class via DI.

What’s Next?

On the next pitstop, we’ll discuss more on the Dagger 2 library — its annotations, how it will make the job of DI easier when we face more complexity.

Please do check my other stories.

Recommended Reading

Thank you for using your precious time to reading this article. Liked it? Clap your 👏 to say “thanks!” and help others find this article.

--

--

Hari Vignesh Jayapalan

Android Dev at WeTransfer| ex Backbase | #1 Google AAD | Public Speaker | Blogger | Creative Coder & Logical Designer