In
this post, we'll take a look at the introduction to the concept and overview of Dagger 2 - a fast and lightweight, which is one of the most popular dependency injection framework currently available for Java and Android.
this post, we'll take a look at the introduction to the concept and overview of Dagger 2 - a fast and lightweight, which is one of the most popular dependency injection framework currently available for Java and Android.
The important question that we need to take a look to understand the Dagger usage is:
What is Dependency Injection (DI) and why do we need it?
In software engineering, dependency injection is a technique whereby one object(or static method) supplies the dependencies of another object. A dependency is an object that can be used (a service).
Dependency injection is built upon the concept of inversion of control(IoC) (i.e. a class should get its dependencies from the outside). In Java, before we can use methods of other classes, we first need to create the object of that class (i.e. class A needs to create an instance of class B). So in more precise, it eliminates the task of creating the object manually and directly using the dependency.
If we create an instance of class A via a new operator in class B, then it can not be tested independently from that class i.e. hard dependency.
So, what would be the benefits of passing the dependencies from outside the class?
One of the most important advantages is that it increases the possibility of reusability of the class and to be able to test them independently of other classes.
Let's go forward with dependency injection exploration.
Dagger version 1.0 - Created by Square
There is a class called dependency container in a framework which was used to analyze the dependencies of a class. With this analysis, it was able to create an instance of the class and inject the objects into the defined dependencies via Java Reflections. This eliminated the hard dependencies and the class could be tested independently, for example: by using mock objects.
Main disadvantages of this process were: First, the Reflection is Slowing itself and second, it used to perform dependency resolution at runtime.
Dagger version 2.0 - Adaptation of an earlier version and maintained by Google
The big changes in Dagger 2 are:
- Generate the dependency graph using the annotation processor.
- The classes providing the dependencies were now generated at build time using javax inject package.
- Facilitated the possible error checking before the application runs with a highly readable generated code.
Dagger uses the standard JSR-330 annotations.
There are three modes of injection:
1. Constructor Injection: Injecting parameters in the constructor.
2. Field Injection: Injecting the member variable(except private).
3. Method Injection: Injecting parameters in the method.
Note: The order of dependency injection according to JSR-330 are constructor, field and method.
Dependency injection process with Dagger:
A dependency consumer use dependency provider to asks for the dependency object through a connector.
1. Dependency provider: Classes annotated with @Module provides or builds the objects dependencies. Such classes define methods annotated with @Provides and return objects which are available for dependency injection.
2. Dependency consumer: Classes annotated with @Inject.
3. Connecting consumer and producer: A @Component annotated interface defines the connection between the provider of objects(modules) and the objects which express a dependency.
Reference Links:
Comments
Post a Comment