Dagger 2 Android : Defeat the Dahaka

Best Practices / Code Quality

Session abstract: 

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 classes)”. 

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.

Creating a dependent component? You should create another scope!! And if this is not enough Create some subcomponents, create Qualifiers, use Lazy injection, use Provider, use static Injection, use Async Injection.

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 Module and a Component? 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 DaggerComponent which has a "has-a” relationship with our Module. So far so good? Here comes “The Dahaka” (an infinite storm in an ocean of Providers, Factories, Builders, MemberInjectors, DoubleChecks, Lazy, Producers... ) 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 (http://www.imdb.com/title/tt0384444/quotes)

In this talk I aim to Defeat “The Dahaka (a storm in the ocean of classes and Generics)” or at least try to tame the beast. I will firstly go over the basics of Dagger and try to unleash the beast by going into the generated classes and finding out the patterns. I will then talk about applying those patterns to gradually learn the technicalities of Dagger Android. How can we 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 attending this talk you will get a better sense of what goes on behind the scenes and how to smartly move towards Dagger Android and still keep your cool :)

For more information, lookout for the series “Dagger and The Dahaka” on medium : https://medium.com/@ragdroid

Here’s how I am going to tame the Beast : 

  • Introduction : Quick walkthrough of The Basics (Codes of Time)
  • Definitions : A sneak peek at the generated code (Face of the Dahaka)
  • DoubleCheck: A deadly dive into Scopes (Unleash the Beast)
  • Relationships : Components, Subcomponents, Dependent Components, Subcomponent Builder (Defeat the Dahaka)
  • Implementation : Dagger 2 Android! (Befriend the Beast)