🔪 Introduction to Dagger DI 💉 by a Life way!

Subscribe to my newsletter and never miss my upcoming articles

In this article, I’ll explain the basic concept of popular💉 Dependency Injection framework — Dagger 🔪 by using a simple example.

You might hear the terms — Dagger or Dependency Injection. What’s it actually? Why it’s used? Why developers use it? Okay! we’ll see it.

# What is Dagger?

Dagger is a fully static, compile-time dependency injection framework for both Java and Android.

# Okay, then what is Dependency Injection? 😕

Dependency Injection (DI) is a design pattern that allows the creation of dependent objects outside of a class and provides those objects to a class in different ways. Using DI, we move the creation and binding of the dependent objects outside of the class that depends on them.

Still not getting it?😕 Don’t worry

When you are developing a program, you may have classes that depend on other classes for their creation. Sometimes, it becomes complex to create such dependencies. At such times, dagger comes to rescue. Let’s see how 😃.

💭Imagine your Life —

You’ve your Life. It has Basic Needs. Needs include Food, Clothes, and Shelter. These basic needs are possible in your life only if you have Money. Money can be there if you have a good Job. Also, it depends if you have Education.

As above, Life is dependent on many factors. If anyone of dependency is missing, the whole life will become shit! 😢 Else it’ll be enjoyable 😃. Right? Now, how it’s related to DI? Let’s convert your imagination into the code. See code below 👇

We required to manually create each and every object and passed it to the constructor.

In such situations, Dependency Injection comes to the rescue. Let’s start the code 👉

💻 Getting Started

Import below dependencies in your project.

dependencies {
    implementation('com.google.dagger:dagger:2.15')
    annotationProcessor('com.google.dagger:dagger-compiler:2.15')
}

In your code, use @Inject to annotate the constructor that Dagger should use to create instances of a class. When a new instance is requested, Dagger will obtain the required parameter values and invoke this constructor. Let’s make Life classes one by one…

  • The base of our Life is Education

Here we’ve injected constructor. It means we are telling dagger that to create its instance.

  • Now Job is dependent on Education
  • Money is dependent on Job —

Remember, we’ve annotated this class as @Singleton to ensure the single instance of Money throughout Life program.

  • Now Food, Clothes, and Shelter are dependent on Money.
  • Then Needs are Food, Clothes & Shelter —
  • Finally, Life has Needs —

You can see, we have annotated constructor by @Inject . Dagger will take are to create an instance of these dependencies.

Make Life Component —

Create an interface with a method getLife() which will return Life instance.

Now Dagger will take care of making Life and its dependencies. 😃

Just Build ⚙️ ️your project so that the dagger will generate classes.

Now you’ll surprise after seeing the simplified code after using Dependency Injection. Here’s code after using Dagger DI framework 👉

Dagger internally created Life along with all dependencies. Finally, if you run this code, you’ll see output —

I'm Well Educated!
I've Job!
I've Money!
I can eat delicious dishes!
I've Clothes to wear!
I've my own house to live!
I'm enjoying my life! :)

There are no chances that it would fail. In manual DI, it may fail if we by mistake pass any dependency as null.

Yeah 😍! It’s working as expected. Hope you liked that. If you find it helpful please share this article. Maybe it’ll help someone needy!

Sharing is Caring!

Resource —

Here is a repository that contains the code used in this article.

Thank you 😄!

If you want to contact me, feel free to reach me…

.

No Comments Yet