What is Dependency Injection using Dagger?

Sumeet Panchal
3 min readJan 11, 2020

--

Dependency injection is nothing but a design pattern which allows us to remove the hard-coded dependencies and make our application loosely coupled, extendable and maintainable.

Let’s take an example to understand it more clearly.

We have two classes called Class A and Class B.

Figure: 1

In the above Figure 1, class A has a variable of class B and so class A is dependent on class B. Also we can say that class B is a dependency of class A.

Examples would be :

  1. User class has a dependency of UserAddress class.
  2. RecyclerView class has a dependency of Adapter class [Android Specific]

So to provide the dependency from outside we use Dependency Injection.

Let’s understand it more clearly by taking an example of constructing a Bike.

Pseudo Code using Java.

public class Bike {
private Engine engine;
private Wheels wheels;

public Bike() {
engine = new Engine();
wheels = new Wheels();
}

public void drive(){
System.out.println("Driving the Bike......");
}
}

In the above code example the Bike class has dependency of Engine and Wheels class. If you look at the class there is nothing wrong in this code. It will compile and run successfully.

But if you notice that every time we create an object of class Bike we are creating an object of class Engine and Wheels from scratch, which is not the same in the real world scenario. Whereas the Engine and Wheels are created separately(tested) and then injected in the Bike class from outside.

Let’s modify the above code sample

public class Bike {
private Engine engine;
private Wheels wheels;

public Bike(Engine engine, Wheels wheels) {
this.engine = engine;
this.wheels = wheels;
}

public void drive(){
System.out.println("Driving the Bike......");
}
}

If you look at the above code sample, now the Bike class does not have to create the Engine and Wheels class from scratch. This mechanism is called constructor injection. The Engine and Wheels object needs to be provided by the caller class.

You can also use method injection to provide Engine and Wheels object as shown in the below sample.

public class Bike {
private Engine engine;
private Wheels wheels;

public Bike() {
// empty constructor
}

public void setEngine(Engine engine) {
this.engine = engine;
}

public void setWheels(Wheels wheels) {
this.wheels = wheels;
}

public void drive(){
System.out.println("Driving the Bike......");
}
}

The above example shows how to inject dependencies using methods.

Now let’s make the bike and start driving…….

public class MakeBike {
public static void main(String[] args){

Engine engine = new Engine();
Wheels wheels = new Wheels();

Bike bike = new Bike(engine, wheels);
bike.drive();
}
}

In the above sample code, we have only dependency of Engine and Wheels class. But suppose Wheels and Engine both have some dependency on multiple classes. See the code below.

public class MakeBike {
public static void main(String[] args){

CylinderBlock cylinderBlock = new CylinderBlock();
Piston piston = new Piston();

Alloy alloy = new Alloy();

Engine engine = new Engine(cylinderBlock, piston);
Wheels wheels = new Wheels(alloy);

Bike bike = new Bike(engine, wheels);
bike.drive();
}
}

Imagine so much boiler plate code we needs to be written in a real world scenario just to create a Bike class.

To Avoid creating boilerplate and make the code readable we use Dagger.

So what is Dagger ?

Dagger is a fully static, compile-time dependency injection framework for both Java and Android. It is an adaptation of an earlier version created by Square and now maintained by Google.

It uses annotation processing to create the dependency related code.

Dagger helps to remove and maintain the boilerplate code as shown in the below example code.

public class MakeBike {
public static void main(String[] args){

Bike bike = DaggerAppComponent.createBike();
bike.drive();
}
}

So with the above code sample you have must have understood how the code have been reduced and its much more readable as compared to the above code samples.

Dagger has few main annotations that can be used to create dependency injection code.

  1. @Component
  2. @Module
  3. @Inject [This is java annotation]
  4. @Provide
  5. @Subcomponent ……. and more.

This ends our discussion on Dependency Injection and Dagger. Thanks for reading please share any comments or suggestion.

#Android, #Java, #Programming concepts, #DaggerBasics

--

--

Sumeet Panchal
Sumeet Panchal

Written by Sumeet Panchal

Programming enthusiast specializing in Android and React Native, passionate about crafting intuitive mobile experiences and exploring innovative solutions.

No responses yet