Dagger 2 Android : Defeat the Dahaka
Garima Jain
@ragdroid
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.
Outline
- Starting to learn about Dagger? Start by creating a
@Module
and a@Component
. Add a@Singleton
scope 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
Qualifiers
, useLazyInjection
,ProviderInjection
,staticInjection
,AsyncInjection
,MultiBinding
and 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 aModule
and aComponent
?
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 everyComponent
, Dagger generates aDaggerComponent
which has a “has-a
” relationship with ourModule
. - 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 :)
TL;DR
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
Factories
MemberInjectors
ProviderInjections
LazyInjections
AsyncInjection
. - *“Unleash the Beast” : Relationships *: A Tale of
Scopes
,Components
,Subcomponents
,Dependent Components
and theBuilders
. -
*“Defeat the Dahaka” : Battle *: Let’s use the tools at hand like
Builders
@BindsInstance
Component.Builder
Subcomponent.Builder
DoubleCheck
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 😃
Requirements
Lots of excitement and an open-mind :)
Speaker bio
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.
Links
- First article about talk “Dagger 2 : Component Builder” : (https://medium.com/@ragdroid/dagger-2-component-builder-1f2b91237856)
- Second article about the talk Dagger 2 : Check — SingleCheck — DoubleCheck … Scopes
- Main article about the talk “Dagger 2 Android : Defeat the Dahaka” : (https://medium.com/@ragdroid/dagger-2-android-defeat-the-dahaka-b1c542233efc)
- Articles about the talk featured in Android Weekly : http://androidweekly.net/issues/issue-265
- Rxify : Droidcon Boston, 2017 : (https://news.realm.io/news/rxify-rxjava-operators-droid-boston-jain-2017/)
- Android Dialogs : (https://www.youtube.com/watch?v=5JO7Q6HO52w&t=1s)
- Rxify : Workshop, GDG New Delhi : (https://www.facebook.com/events/1846568045632997/)
- Rxify: Droidcon India, 2016 : (https://www.youtube.com/watch?v=UTyPMjfO1cA)
- Rxify : Github Demo : (https://github.com/ragdroid/rxify)
- Mockstar : Github Demo : (https://github.com/ragdroid/mockstar)
- Fueled : (https://fueled.com/projects/)
- Medium : (https://medium.com/@ragdroid)
- Twitter : (https://twitter.com/ragdroid)
{{ errorMsg }}