Dagger 2 Made Simple In Android – Part 1

Dagger 2 Made Simple In Android – Part 1

Howdy guys, I have decided to give you my perspective on Dagger 2 and its implementation using the simplest words possible and avoiding all the jargon. This will be a short series about Dagger, remember to follow the next articles 🙂

What is DI?

Dependency injection is a very simple and common practise that we do everyday without realizing it. But when the big term (Dependency Injection) is used, is appears as though it’s a monster we’ve never heard of.

In simple terms, DI means supplying an Object whatever it is that it needs to do its job. For example:

Our object Person will require String (name) in order to do its job. Let’s use DI to solve this.

Voila! We’ve just performed DI. We’ve satisfied Person’s object dependency by giving it what it needs. This is awesome. We have one problem though, we have given Person object the responsibility of creating its own dependencies, that is bad practice.

So, how do we solve this?

Somebody call Dagger!!!

Diving into Dagger 2

Dagger helps solve this problem by creating the dependencies for us after which we just pass them to our objects, hence reducing their work and make them focus on their real tasks.

Dagger has 3 building blocks :

  1. Scope
  2. Module
  3. Component

Scope

Well, scope is just scope, or in other words, the boundary in which the instances or dependencies created by Dagger will cover. We can have an Application Scope for dependencies that last throughout the application lifecycle, or we can have Activity Scope and even Fragment Scope as well, depending on your needs.

Most people like to use the @Singleton annotation for dependencies that last throughout the Application Scope, but it is recommended to create your own scope (there’s a reason). This is how you can create a scope.

Module

This is the guy who will provide or give us all our dependencies. But since dagger is not magic, we have to tell it how to create those dependencies. Let’s take a look.

As you can see, a module requires a couple of things. First, you should annotate it @module for Dagger to know that this is a module. Second, it needs an @scope annotation for it to know the scope of the dependency and lastly an @provides annotation for every method/function that will provide a dependency.

Inside these methods is where you tell Dagger how to create your dependencies.

Component

This is now the link between our dependencies(from the module) and Dagger itself.

It also requires some few things. First, the scope and in this case we want our dependencies to last throughout the app’s lifecycle hence we use @ApplicationScope. Second, it requires an @Component annotation to tell Dagger that this is a component.

Inside the @Component annotation, you give it your Module Class(es). Lastly, expose the dependencies that you will need to access from other parts of the application.

Final Steps

Finally, you will need to build your project in order for Dagger to generate the classes it needs to do the job 🙂

As you can see Dagger generates a class for your component with the name Dagger in front of your component name. Since dagger uses builder pattern, you can chain methods on it to create your module and then satisfy its dependency (our module depends on context) by passing this, which is an application context in this case.

Summary

There you go folks! That is Dagger in a nutshell. Next we will talk about some common Dagger errors and how to solve them, then dig deep into more complex Dagger implementations than this. So stay tuned!

If you found this helpful, please share (Blessed are those who share!).

Remember to leave your opinions, remarks, questions or a comment and I will be glad to answer and help where I can. Thanks for your time, appreciated. Cheers!!!

Happy Coding!

2 Replies to “Dagger 2 Made Simple In Android – Part 1”

  1. Great tutorial! At least am motivated to start using Dagger 2. Am eagerly waiting for the second part of the tutorial.

Leave a Reply

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