Dagger, but for 3 year olds
Let’s make it easy
From writing my first activity, to writing production level code, it has been quite fascinating. Even more, to see widely available online resources to learn almost everything related to Android Development. But few things are still not easy to wrap your head around, one of which is Dagger
.
When I first, heard of Dagger
, I visited its official docs & usages. But got nothing! I continued development, unless I crossed paths a couple of times more. It was only after third time that I got some determination to go through it. Still, it wasn’t a smooth road. Tens of articles, hours of videos. Each gave me only about 5% to proceed with, but never the full story (or at-least that’s what I was able to grasp). But with time, and stubbornness I was able to get a hold of it.
With this series of articles, I aim to pave an easier road for others, who are stuck with Dagger like I was. Also I assume that you know what LEGO
is.
Let’s go!
Before we start
Any article on Dagger includes many words which you may not have heard of. Seeing which might make it overwhelming. So, let us tackle the important ones first.
Dependency Injection(DI)
is a method in which, the requirements of a class(dependencies) are fulfilled by some other external entity. (Will go in detail very soon, don’t worry if you did not understand)Dependency Graph
hierarchy of dependency relations . For ex. A depends on B, B depends on C, and so on.Annotations
acts as a metadata for the written code, ex.@PrimaryKey
,@Entity
etc.
That’s all you need to know. Let’s have our first bite at Dagger now!
Deep dive
Imagine that you live in a LEGO world. Everything around you is LEGO Blocky
. Amazing world indeed, right?. But now imagine that every time you think about having food, you have to walk to the kitchen, then plates needs to be assembled. Then the food. Then spoons. Just like a new box of LEGO. Its almost like:
when(wantDinner) { walkToKitchen()
plate = makePlateLEGO()
food = makeFoodLEGO() eatDinner()
}
While, assembling a LEGO is fun in itself, it can become repetitive and time-consuming really fast. Now bring this over to Android side. If you aren’t using Dagger it might be the case that every time your object needs something, it has to either assemble it by itself, or rely on some other object to provide ready-made dependencies to it.
This works fine. But with Dagger
the writing part can be decreased even more, leading to faster and more manageable development.
Now, let’s see how Dagger would improve our LEGO life!
D.I. all the day!
Instead of you going to the kitchen every time, wouldn’t it be nice if you just double clap and the food starts to automatically assemble and then gets presented in front of you? This inversion
of the method for providing dependencies is called Dependency Injection
.
“With DI, you don’t go to your food, your food comes to you“
Now replace “You” with “
Your objects
”, and “your food” with “their dependencies” in the above sentence. That’s DI.
In-short, Dagger implements the DI methodology in Android.
Okay… but how?
Dagger’s implementation require 2 entities — Modules
and Components
.
Modules
are responsible for constructing the various objects or dependencies. In LEGO-analogy, Modules
are the machines that assemble LEGO pieces to make your favorite food items. In Android world, this means methods that construct & return the required objects, such as Retrofit, RoomDB, etc.
Components
are the bots, that deliver the assembled LEGO food to you while you rest on the couch, i.e. it tells where to take the above dependencies. In Android, this translates to methods, with injectable classes (target) specified in its parameters.
Remember annotations
? They act as glue that connects the components
with modules
and the injectable targets, to make the overall system work.
I see no code :(
I feel like, getting a grasp of Dagger’s bare-bones concepts is a must before overloading ourselves with its implementation. This helps in understanding the working of Dagger more clearly. (Divide & Conquer)
I will explain the implementation details with code in the next article. So make sure to read that, once you are clear with the concepts mentioned over here. Do you think this approach is better? Clap if you learnt something new today!
Find the sequel at:
If you feel your concepts are strong enough, I leave you few pieces of code to figure out the relation between them. I have explained these in the next one :)
Until next time. Adios!