Dagger : A dependency injection framework

Posted By : Bipin Tiwari | 24-Jun-2022

What is Dependency Injection : 

 

Dependency Injection is a programming paradigm that allows us to make a class independent of its dependencies. It helps us to follow the SOLID principle's DEPENDENCY INVERSION and SINGLE RESPONSIBILITY


Using dependency injection, we can separate the concerns of creating an object and instead provide them, which leads to loosely coupled programs.

 

In short : 
"Dependency injection is providing the objects/dependencies that an object/dependent needs instead of having it construct them itself. It's a very useful technique for testing since it allows dependencies to be mocked or stubbed out."

 

Let's see an example : 

 

Take a Car class : 

class Car{
    private Wheel wh = new TubeWheel();
    private Battery bt = new LBattery();

    //The rest
}

Here the Car is dependent on the Wheel and Battery class. But what if we need to create a Car with TubeLessWheel, then we need to create another Car class with a TubeLessWheel class as dependencies.

 

So how the dependency injection helps us?

We can simply inject the Wheel and Battery class via constructor or field injection without changing anything in the Car class. Like this : 

 

class Car{
    private Wheel wh; // Inject an Instance of Wheel (dependency of car) at runtime
    private Battery bt; // Inject an Instance of Battery (dependency of car) at runtime
    Car(Wheel wh,Battery bt) {
        this.wh = wh;
        this.bt = bt;
    }
    //Or we can have setters
    void setWheel(Wheel wh) {
        this.wh = wh;
    }
}

 

Here one more principle is followed in clean code that is "Code to interface not to an implementation". It even allows us to mock the object if we want to test out our code.

 

So where does Dagger come into the picture?

Dagger is a fully static, compile-time dependency injection framework for both Java and Android. It is developed by the Java Core Libraries Team at Google. 


Dagger doesn't use any reflection or runtime bytecode generation instead it does all its work at compile time and generates a plain Java code.

 

So let's look at how Dagger works.

In Android Development to use Dagger, we use its annotations to create a graph and provide dependencies. Some of the annotations are @Module, @Inject, @Provides, @Scope, @Qualifier, etc. These allow us to avoid repetitive codes.

 

Dagger Setup : 

1. Add the dependencies in app build Gradle :     

 

/* Dagger2 - We are going to use dagger.android which includes
 * support for Activity and fragment injection so we need to include
 * the following dependencies */
implementation 'com.google.dagger:dagger-android:2.17'
implementation 'com.google.dagger:dagger-android-support:2.17'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.17'

/* Dagger2 - default dependency */
annotationProcessor 'com.google.dagger:dagger-compiler:2.17'

 

and now we are ready to use Dagger.

 

Next time we will look into setting up modules, entities, and components.

 

If you want to know deep about it, the documentation can be a bit of help: Dagger

About Author

Author Image
Bipin Tiwari

Bipin is a highly skilled Android developer with extensive professional experience in creating innovative and efficient mobile applications. He possesses proficiency in programming languages such as Java and Kotlin, along with a deep understanding of Android SDK, design patterns, and best coding practices. Bipin is committed to delivering high-quality code and ensuring exceptional user experiences. With excellent problem-solving skills and the ability to work collaboratively in a team environment, he has made valuable contributions to both client and internal projects, including the development of the Blackbook Travels App and Corniz App.

Request for Proposal

Name is required

Comment is required

Sending message..