Guide For Dependency Injection Using Dagger2

Hey! Finally I decided that now a good time to get back to the blog and share what I have dealing with for last few weeks. Well I am working on Dependency injection using Dagger 2. So let's start with it what it is Dagger 2 ? and why you require to learn. during this process of learning i have googled it lot but i couldn't find proper explanation about this library and from where to start with it. So i thought it would be good if i write some thing simpler that helps others in understanding WTF going on.

Dependency Injection Using Dagger2

Quick Overview

Dagger 2 is dependency injection framework. if you don't know about dependency injection you should check this video. Dagger2 is developed by Google and it is a forked from Dagger 1. which was created by Square.

In Dagger 1 All this process happens at run time. while in case of Dagger 2 all: graph validation, configurations done at compile time.

Dagger 2 generally for code generation and is based on annotations (explained below). During program execution object graph gets created by your application. And using abstraction your application create such a dynamic flow by observing object interactions.

Dagger 1 and 2 are based on standard set of annotations (and one interface). use on injectable classes in order to to maximize re usability, test ability and maintainability of java code.

I won't talk more about Dagger 1 because it is now no more in use because Dagger2 have increased speed in injection and also allows the use of proguard.


@Scope: Scopes are very useful and powerful way to define scope or let's say availability of any particular object. Dagger 2 has a more concrete way to do scoping through custom annotations. It can be @PerActivity, @PerFragment etc. By default it provide only @singletone scope for root component. We will see this in an example later on.

@Module: Used in classes and methods which provide dependencies. Module class provide all the dependency objects when you require and when you use @Inject annotation.

@Provide: Inside modules we define methods containing this annotation which tells Dagger how we want to construct and provide those mentioned dependencies. it i used in Module class to provide particular object.

@Inject: Request dependencies. It an be used on a constructor, a field, or a method. Dagger2 will inject object into class from specific modules.

@Component: It is main an interface from where dagger2 create object graph for module and injected classes. It enable selected modules and used for performing dependency injection. Such an interface is used by Dagger 2 to generate code.

These are the common but not all annotations for Dagger2. but for starting with it you have to learn this much only. later on next part of tutorial i will go in deeper. this is just for staring and understanding purpose. because i think its good to know how library works and weather it is useful to you or not. in next tutorial i will explain how you can add Dagger2 into your project and from simple object injection to more complex structure for dependency injection.

The official source for Dagger 2, with some documentation of it’s own:

Keep coding...


Post a Comment