First part of my Dagger 2 story — Component and Inject

Onur Şeref
Loodos
3 min readJul 25, 2019

--

I am not going to be writing the theory or details of Dagger. A lot of people think Dependency Injection and Dagger is hard to learn while reading some stuff about it so I would like to focus on practical examples, how we can use it on our applications.

Dagger 2 is a dependency injection framework for Android and Java and it is developed by Google but we can also use it with Kotlin.

First of all, I would like to start with a well-known example of what is a dependency. An Image loader like Picasso uses an HTTP client to load the image from URL. This means HTTP client is a dependency of the Picasso. When we pass the context to a method or constructor, we inject context as the dependency.

We have to do a lot of initialization. When our Cigarette created it is of dependencies. As we see our Cigarette has its own dependencies like nicotine, carbon. This can be at different places in our project. When we change constructor, we must change it everywhere. That makes too many boilerplate codes. Dagger helps us to creates these at the right time to use.

Component Class and Inject Constructors

@Component creates and stores our objects and provides them to use.

We had added @Inject annotation to our constructor. Creating these dependencies inside the Smoker class we should pass it constructor or inject them. When we use Inject, Dagger provides these to use whenever it needed. This is why we have also annotated constructors of Cigarette. Injecting them save lots of boilerplate code. When we try to build our code in Android Studio, That will not build it. Because we did not @Inject Cigarette constructor. We have to do the same thing in Cigarette class.

I have created smokerComponent from DaggerSmokerComponent which was generated at compile-time and accessed to getSmoker() in MainActivity.

Field Injection

As I said first part this story I am not going to talk about theory and details. I am going to focus usage Dagger 2. So I will give a simple example of how we use in our all apps.

I have created an Application class to access it from everywhere.

We have to pass the application to injectApplication method and call it to our component. This process is called “Field Injection”. We call Dagger to take our application and inject it to our variables which are annotated with @Inject . Making this with just one method in our component class as injectApplication helps us to add multiple variables and we can use these variables when we need.

Method Injection

I created Lighter class to fire lighter before smoker smokes.

When we inject a method, that method will be executed automatically after constructor created. If we inject multiple methods Dagger will call one after another. App will run Smoker constructor after that startSmoke method will be executed automatically to fire lighter. We can also combine these with field injection. If we had field injected variable it could be called after constructor called.

I will continue with the next parts of my Dagger 2 story.

Thanks to reading…

--

--