Dependency Injection With Dagger 2 For Beginners — Part 2

Dependency Injection With Dagger 2 For Beginners — Part 2Fazal HussainBlockedUnblockFollowFollowingMar 3Dependency Injection AndroidWhat 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.

Dagger aims to address many of the development and performance issues that have plagued reflection-based solutions.

What is a Component?The most important piece of a Dagger is a Component.

It is also called as a backbone of Dagger.

Components create the dependency graph it knows that our Car class needs the Engine and Wheels.

It is a Direct Acyclic Graph.

Direct means it is going in one direction.

Acyclic means there is no cycle.

Nothing is the child of itselfDirect Acyclic GraphThe Component is an interface that stores the object and provides to us.

It is also called as Injector.

The component provides the dependency in two ways either from the member variable or from the getter method.

The component is a dagger annotation that implements the below interface and generates the necessary code automatically.

We don’t need to specify how this method works.

Dagger does all off for us.

@Componentinterface CarComponent { fun getCar() : Car}Constructor InjectionTell the dagger that how to create an object.

There are multiple ways to inject the dependency and constructor injection is one of them.

Using the dagger Inject annotation to tell the dagger that you can instantiate the object in this way.

class Car { val TAG = "Car" var engine: Engine var wheels: Wheels @Inject constructor(engine: Engine, wheels: Wheels) { this.

engine = engine this.

wheels = wheels } /** * Drive car */ fun drive() { Log.

d(TAG, " Car Driving.

") }}How to access the car object using the component?.When you compiled your project after creating a Component.

Dagger will create a class starting with Dagger followed by your Component interface.

In this class, Dagger generates a necessary code for you.

public class MainActivity extends AppCompatActivity { private Car car; @Override protected void onCreate(Bundle savedInstanceState) { super.

onCreate(savedInstanceState); setContentView(R.

layout.

activity_main); CarComponent component = DaggerCarComponent.

create(); car = component.

getCar(); car.

drive(); }}Field InjectionTry to use constructor injection whenever possible because it is pretty simple.

But some times constructor injection is not possible or not sufficient.

But there is some boilerplate code.

We don’t have to instantiate each class.

We can use field injection or member injection.

public class MainActivity extends AppCompatActivity { @Inject private Car car; @Override protected void onCreate(Bundle savedInstanceState) { super.

onCreate(savedInstanceState); setContentView(R.

layout.

activity_main); CarComponent carComponent = DaggerCarComponent.

create(); car.

drive(); }}Run your project you will get the error: Dagger does not support injection into private fields.

Dagger does not support injection into private fields.

Remove the Private Access Modifier still dagger is unable to inject even it is injected with the Inject Annotation.

Go to the component interface create a new method called as inject.

Note that the inject method will take the main Activity.

You can not supply the parent activity like AppCompatActivity or Activity.

/** * Component interface that is responsible for creating dependency graph.

*/@Componentinterface CarComponent { /** * Component just create car object on thier end and provide a car * object at compile time instead of creating object at * run time to avoid crashed and low performance.

* * @return [Car] object */ fun getCar() : Car fun inject(mainActivity: MainActivity)}Now go back to our MainActivity.

Call the inject method and now dagger knows the MainActivity and injects the member annotated with Inject Annotation.

public class MainActivity extends AppCompatActivity { @Inject Car car; @Override protected void onCreate(Bundle savedInstanceState) { super.

onCreate(savedInstanceState); setContentView(R.

layout.

activity_main); CarComponent carComponent = DaggerCarComponent.

create(); carComponent.

inject(this); car.

drive(); }}The whole process is known as field injection.

Finally, run your project and check the logs.

Final OutputPart 3In the next part, we will discuss the situation when we don’t have access to the class i.

e external or third-party libraries.

We will cover the topics i.

e Modules and Providers in Dagger 2.

. More details

Leave a Reply