Dagger 2 Android : Defeat the Dahaka
We have had many talks and blog posts on Dagger and Dependency Injection till now. I must say that people have done a great job at explaining various concepts of dependency injection, it’s technical details and also case studies. But, in my opinion, Dagger is something which has become a part of our coding lives now. Yet, from time-to-time we still find ourselves lost in the world of dependencies and dealing with “The Dahaka” (a beast of generated code).
In this talk, we will aim to Defeat “The Dahaka” (a storm in the ocean of generated classes) or at least try to tame the beast. We will firstly go over the basics of Dagger2 and try to unleash the beast by going into the generated classes and finding out various patterns. We will then go into the details of those patterns and learn how to apply them. We will then find our way towards the technicalities of Dagger Android. Also how to move towards Dagger Android without blindly applying those annotations and also trying not to die in the hands of “The Dahaka” by putting the leash back on the Beast.
- Starting to learn about Dagger? Start by creating a
@Component. Add a
@Singletonscope to it.
- Dependencies getting out of hand? Create another dependent component or a sub-component.
- Creating another component? You should create another scope!! And if this is not enough Create
MultiBindingand what not!
- Still alive? Now we present you with Dagger Android and I am sure that it will certainly kill you :P
But wait! What exactly is the relation between a
We don’t know that. Thankfully! Dagger 2 generates all it’s code at compile time and we can actually see for ourselves what’s going on behind the scenes.
We can see that for our every
Component, Dagger generates a
DaggerComponentwhich has a “
has-a” relationship with our
- So far so good? Here comes “The Dahaka” (an infinite storm in an ocean of Providers, Factories, Builders, MemberInjectors, DoubleChecks, Lazy, … ) You can easily get lost in those classes.
“Most people think dependency injection is like a river that flows swift and sure in one direction, but I have seen the face of dependencies and I can tell you they are wrong. Dependency Injection is an ocean in a storm. You may wonder who I am and why I say this; sit down and I will tell you a tale like none that you have ever heard!” — Quote inspired from Prince of Persia
In this series of blog posts, we will aim to Defeat “The Dahaka” (a storm in the ocean of generated classes) or at least try to tame the beast. We will firstly go over the basics of Dagger2 and try to unleash the beast by going into the generated classes and finding out various patterns. We will then go into the details of those patterns and learn how to apply them. We will then find our way towards the technicalities of Dagger Android. Also how to move towards Dagger Android without blindly applying those annotations and also trying not to die in the hands of “The Dahaka” by putting the leash back on the Beast.
After reading these series of articles you should get a better sense of what goes on behind the scenes and how to smartly move towards Dagger Android and still keep your cool :)
Here’s how we are going to tame the Beast :
- “Codes of Time” : Introduction : We already have really great articles about Dagger2 basics. This will be a collection of my favorites references and a quick walkthrough of the basics.
- “Face of the Dahaka” : Definitions : A sneak peek at the generated code and various Dagger terms and patterns. Let’s swim in the ocean of
- “Unleash the Beast” : Relationships : A Tale of
Dependent Componentsand the
“Defeat the Dahaka” : Battle : Let’s use the tools at hand like
Multibinding… in order to tame the beast. Dagger 2 : Component.Builder
“Befriend the Beast” : Implementation : Dagger 2 Android 😎!
Checkout the Series of articles on Medium:
- Here’s the very first article “Dagger 2 : Component.Builder”
Here’s the second article “Dagger 2 : Check — SingleCheck — DoubleCheck … Scopes”
Here’s the Main Article : “Dagger 2 Android : Defeat the Dahaka”
I am also giving a similar talk on “Dagger 2 Android : Defeat the Dahaka” at Droidcon Berlin, 2017 😃
Lots of excitement and an open-mind :)
Garima is a computer science post-graduate from IIT Jodhpur and currently working as an Android Developer at Fueled, Noida having an experience of 4 years in Android Development. She is an active community speaker and tech-fi blogger who believes in sharing and learning with the community. Her journey of community sharing started with Droidcon India, 2016 and since then she has posted multiple blog posts on her medium account and spoken at Droidcon Boston, 2017 and taken a workshop at GDG New Delhi. She is also speaking at Droidcon Berlin, 2017.