Dagger 2 — Easy Peasy — Storified

They say if you are dependent on anything, you will fail somewhere. Is that right? Let’s explore…

Understanding Dagger is a bit clumsy task, you know why? because it has a different learning structure than most of the programming elements. When I was first learning dagger I thought it might come hard and it did, but the practice was what kept me going and I understood a better way to express it as well.

Some Bookish Therotical Thing which we all ignore but it has to be known.

Dagger 2 is dependency injection framework. It is based on the Java Specification Request (JSR) 330. It uses code generation and is based on annotations. … Dagger 2 uses the following annotations: @Module and @Provides : define classes and methods which provide dependencies.

Starting it, let’s say you are a manager @ Dominos somewhere, now a customer orders you a PIZZA which certainly is Non-Veg Loaded+ Cheeseburst. Tastessss awesomeee mann!😍

You’d tell your chefs working inside a kitchen to make it.

Inside the kitchen:

A guy is assigned to get some chicken, some flour, and some milk for cheese, you start making cheese from scratch, marinate chicken and then make the dough then you build your own oven and then bake it and serve.

Cooking time: 2 days. Such a long story? I’m sure if I’m the customer I would never come to eat again.

Hence, Dominos got a point and they removed all these things by what? brought marinated chicken, brought pre-made cheese, etc then just gather things into one, bake it, and served.

Cooking time: < 30 mins

Which way is good? I believe the 2nd one since it had less dependency and If you feel that the 1st way is the better one then we can’t be friends 😔.

Same is the case with our codes, they don’t want to keep on creating everything when its the showtime, instead let’s bring it from somewhere else.

Let’s start it in code now, make a PizzaActivity.

Understand this that I have a Pizza class that requires toppings and base type to be brought and don’t have to make it every time the customer wants.

If I would have gone the normal way (1st way ) of making topping and base, it would look something like this:

Let’s not make pizza topping and base inside Pizza class instead get it from somewhere. But where?? WE NEED A STOCK which is called Module in Dagger.

@Module: This annotation tells the compiler that this class is will have resources which will be used by classes/

@Provides: This annotation tells that the below method is going to be injected or used by a different class.

@Singleton: Who likes to make objects every time, even god made us once. This annotation won’t let create multiple objects.

But in order to get things(Objects) from resources(Module) we need to have a manager, you can’t just directly go into a bank and deposit your money into their cash storage, there needs to be a medium(cashier) for this, which in Dagger is called as a Component. But the component needs to know which module it has to operate on :

Here’s the hierarchy:

A component is nothing but an interface that acts as a bridge between the requesting class(Activity | Fragment) and the providing class(Module).

Here’s our component interface for the Pizza thing which is annotated by @Component which requests and gets an object from the declared module using (modules = [PizzaStock::class]) beside the @Component annotation. You can have multiple modules as well.

Build the project :D

In order to really inject the objects we have to define its container with @Inject annotation

Let’s inject PizzaTopping and PizzaBase into Pizza class but for that until this Dagger will only generate the objects but we want to use it which can only happen when we make use of its component which dagger has generated like this:

DaggerPizzaComponent.create.inject(this)

In the above class, our injection is done now you use however you want to use it. Works charm! :D :D

The same is the case with android, whether you want to inject a ViewModel or a NetworkHelper, do this way.

There’s a lot more in Dagger but this is enough to kickstart :D

Thanks for reading…..

📱Building Android Apps | Kotlin | Java | Open Source Enthusiast | Startup Enthusiast

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store