Back to Blog Archive

Dagger 2 tutorial: Dependency Injection made easy

Posted on: May 11, 2018
Author:
Jose M

Dependency Injection is a difficult concept to understand. If you are tired of seeing dependency, injection or dagger, or just see random words everywhere then you have just arrived at the correct article.

First let’s define what dependency and injection means.

Dependency and Injection

Dependency means to depend and Injection means to inject something into something else. Easy, then when you combine both it becomes Dependency Injection meaning that you can inject dependencies.

But what does it mean in code? What are dependencies in code? Let’s do a class diagram:Dependency Injection graph

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 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 Body to create Blood but this would create boilerplate code. It is better if we just inject it through a dependency injection framework, that is where Dagger 2 comes in.

Let’s continue with 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:


Dagger injects the class using the annotation @inject, it can be used in a constructor:

In this way, Dagger injects the Body class into another class when needed by Injection. For example; let’s say that we have a class Person that needs injection of a Body, we can do this:

Since Body is not a singleton, what dagger does is to create a new Body object every time that a Person is created by Dagger. Of course every person has a different body so this is okay.

Now let’s inject Blood into the Body, all Bodies have blood:

And now let’s create the class for Blood:

This one is an abstract class and its subclass extends Blood and gets the type of blood as 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 Blood to 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 Blood 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 @provider in a method, it’s best practice to suffix the class with Module and the method with prefix; Provider, as shown here:

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 which specifies what 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 Body using the RandomInjectionModule. It means that a Random blood is injected in the Body automatically by the dependency injection framework that we specified using the @injection in 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 and it has different methods to set different instances of our modules. As 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 what blood class was injected.

Singleton

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

Notice that to use singleton you must instantiate the component project just once. Maybe 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:

https://github.com/jose-mgmaestre/InjectionExample

Conclusion

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

https://google.github.io/dagger/users-guide

I hope that this tutorial has been useful to you. Please don’t hesitate to ask if you have any queries or suggestions.

 

 

 

Author:
Jose M

Comments

Contact Us

Ricston Ltd.
Triq G.F. Agius De Soldanis,
Birkirkara, BKR 4850,
Malta
MT: +356 2133 4457
UK: +44 (0)2071935107

Send our experts a message

Need Help?
Ask our Experts!