Dependency injection is a difficult concept to understand. If you are tired of seeing dependency, injection, dagger, or even random words in Java, then, you have found the right article for you!

First, let’s define dependency and injections in Java.

Dependency and Injection

As the words indicate, dependency means to depend, and injection means to inject something into something else. Easy, right? When you combine these two things together, it becomes a "dependency injection," meaning that you can now inject dependencies.

But, what does it mean in Java? What are dependencies in code? Let’s do a class diagram:

Here, we can see all the dependencies, and we can review the diagram if we have any doubt during coding.

In the diagram, we have a class Body that depends on a class Blood. Just like in real life, Body will have Blood as a member inside of its class (Body depends on Blood). In addition, we can inject the blood into the body using an injection. Of course, we could create a factory inside Bodyto create Blood, but this would create boilerplate code. It is better that we just inject it through a dependency injection framework — this is where Dagger 2 comes in.

Let’s continue our discussion on how we can inject the Body into a class by using Dagger.

Dagger and Dependencies

Dagger is a dependency injection framework for Android, and, here, we see how to use and configure it.

Firstly, we need to add Dagger to our project using Gradle dependencies:

This one is an abstract class and its subclass extends Blood. It will get the type of Bloodas a string. Every type of blood returns a different letter. You can review these types in the GitHub project at the end of the article.

Okay, you may think, "Good — we have everything set. The Blood will be injected into the Body, but… how?" We have 4 types of Blood: A, B, AB and O. Blood is just an interface. How does Dagger know which Bloodto inject? The answer is that it doesn’t know. This will not work; we need some kind of injection module that injects the blood type we need. Therefore, to solve this problem, Dagger introduced a new concept: the Module.

Dagger Modules

A module is a class that injects concrete classes for an interface. It allows different modules to inject different kinds of concrete implementations into our code. In addition, we can plug-out and plug-in different modules by code. In our case, we use a module called RandomInjectionModule that injects random Bloo into the Body. It sounds scary, but it is just a programming example — nobody will die! Notice that we don’t need a module if we don’t need to inject an interface.

Let’s implement a module. We will use the @module in a class and @provides in a method. It’s a better practice to suffix the class with the module and method with the prefix, 'provide', as shown below:

As you can see, a Blood instance is returned as a concrete class. In fact, it will always return the same instance, since it was marked as static.

Now, we need to use the module in our code. To do that, Dagger introduces another new concept: Component.

Dagger Components

A component is an interface that specifies which modules to use and what objects to return for our code. For example, we are in our main method in Java, and we have a Doctor component that uses the random injection module to inject blood:

In the above code, we return a Bodyusing the RandomInjectionModule. It means that a random blood is injected into the Body automatically by the dependency injection framework that we specified using the @injection in the Body. If we returned a Person using the RandomInjectionModule, then, a random Blood and Body will be injected as well.

Now, let’s use our component in our code. Dagger 2 generates a class with the same name as our class, prefixed with ‘Dagger’ when our project is compiled. In our case, it’s called DaggerDoctor. It has different methods to set different instances of our modules. In our case, all dependencies can be constructed without the user creating a dependency instance. The DaggerDoctor component has a create() method that instantiates our module and injects our classes automatically:

First, we created an instance of our Doctor interface using DaggerDoctor through our create() method, which instantiates our module and injections automatically. Then, we can use our Body as normal and get the blood to print out the blood class that was injected.

Singleton

If you want to use singleton to always inject the same instance, we could have used @singleton tag in a class, the @provide method for the case of interfaces, and @singleton in the component as a scope.

In order to use singleton, you must instantiate the component project once. Perhaps, you might want to have a static method to always retrieve the same component for any class that wants to use it. You can find several examples, like singleton, in different branches of my GitHub project.

Conclusion

Dagger 2 can save you a lot of code and time, while also helping to apply the best practices to your projects. For more information, check out the official guide.

I hope that this tutorial has been useful to you. Please don’t hesitate to add any queries or suggestions in the comments below.