Android

Dagger in Action with Retrofit-2, RxJava 2 Part 1

Why should we use it? This may be the first question comes to your mind, r8? isn’t it?

Ok, let me explain it.

I was a beginner in Android App Development. That time learning a new thing was tedious for me. But I overcame it. Day by day, every developer overcomes it. It is the beauty of a developer.

I am working in this field for a long time, but I had’t felt the need to use Dagger in my project. Then I got a chance to look into it. Then I made a demo project, later this will be discussed in this tutorial.

I saw the beauty of using Dagger in a project. It made my project clean. No need to worry about any dependency. If we remove a object that has a dependency on other classes, then it will destroy the dependency hierarchy. Then we have to solve the error for every classes. Using dagger we don’t need to worry about it, even we add a new dependency on any classes, it won’t affect on the dependency hierarchy. We just need to provide information in the Dagger Component to build up the graph tree. That’s it.

This is the beauty, you will fall love into it.

First time, it is difficult to understand the basics but using again and again you will be ready for the big play.

Let’s dive into the beauty of using Dagger.

First we will talk about some basic that we need to understand.

Some keyword that we will use most of the time are given below.

@Inject → lets dagger know how to create instances of a object.

This is field injection using @Inject keyword

 

This is constructor injection.

 

** Classes that lack @Injectannotations cannot be constructed by Dagger.

*** But @Injectdoesn’t work everywhere:

  • Interfaces can’t be constructed.
  • Third-party classes can’t be annotated.
  • Configurable objects must be configured!

For these cases where @Inject is insufficient or awkward, use an @Provides-annotated method to satisfy a dependency. The method’s return type defines which dependency it satisfies.

We instantiate Dagger allows to add these object to its graph using modules. A module is a class that has the @Moduleannotation and defines methods annotated with the @Providesannotation. Annotated methods return instances of classes that cannot support the @Injectannotation.

To understand the scenario see the below example

 

We need retrofit to fetch data from server. But it is a third party library. If we want to add this in Dagger dependency graph. We need to create a module with @Module annotation and create a function that return that Retrofit.

Then when our app needs to work with retrofit, dagger will provide retrofit instance from its dependency graph. With the help of this module it will be available in the dependency graph.

So what have we learn so far?

@Inject and @Provides

Now,

Using Scopes

Sometimes, you might want to provide the same instances of a dependency in a component. For example, database instance or network call instance, their single instance will be used for app component (Full app life cycle). @Singleton is a built in scope annotation. You can’t use same scope annotation for other component.

Now,

Component → To build dependency graph we need a component. We can call that component AppComponent. Modules must be included here, so that dagger knows and build necessary graph for them.

 

SubComponent

All component must be declared in interface or abstract class. If we have different flow in our app, we can divided them using new component. They will be added to App component so that dagger can access them. And we need to use @Subcomponent annotation.

Subcomponents are components that inherit and extend the object graph of a parent component. You can use them to partition your application’s object graph into subgraphs either to encapsulate different parts of your application from each other or to use more than one scope within a component.

 

Leave a Reply

Your email address will not be published.

4 × five =