Skip to main content

Introduction to Dagger - A fast dependency injector

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.

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

Popular posts from this blog

Kotlin Coding Conventions

In this post, I will discuss the Kotlin coding conventions which are a lot similar to other programming languages. So without further delay lets move to our today's discussion. Source code organization Source code organization is one of the first things we need to discuss in Kotlin coding conventions . Just like in other programming languages, an organization of source code is important in Kotlin for a consistent look across all the projects. Directory structure The source files should reside in the same source root as the Java source files, and follow the same directory structure. In pure Kotlin projects, the recommended directory structure is to follow the package structure with the common root package omitted. Example: if all the code in the project is in the "org.example.kotlin" package and its sub-packages, files with the "org.example.kotlin" package should be placed directly under the source root, and files in "org.example.kotlin.foo

Getting Started With Laravel & React.

Getting started with Laravel & React  is easy now with Laravel version 5.5 which is the next long term support (LTS) version of Laravel (the last being 5.1).  In prior versions of Laravel, only Vue scaffolding is included, but with the release of Laravel 5.5, new front-end preset options are available for bootstrap and React including Vue. In a fresh installation of Laravel 5.5, you can easily swap your Vue scaffolding to React scaffolding using the " php artisan preset react " command. The default Mix configuration, components, and all other related files will be updated accordingly. Now to understand it more let's get started with simple Laravel 5.5 & React demo project. First, choose a directory or folder where you want to place your Laravel project then open a terminal and copy the below command: composer create-project --prefer-dist laravel/laravel="5.5.*" DemoProject After successful completion, we need to change our Vue

Kotlin Idioms

Idioms Random and frequently used in kotlin. Creating Data classes in kotlin (POJOs/DTOs) Data classes are model classes which are created to hold data. In Java, we usually create POJO classes with getter and setter methods for each member variable of a class. But in Kotlin, POJO class can be created within a single line of code with keywords: 'data', 'class', 'class name' and its member variables. data class Student( val name: String, val age: Int) provides a Student class with the following functionality: - getters(and setters in case of vars) for all properties - equals() - hashCode() - toString() - copy() - object1(), object2(), ..., for all properties. Filter a list in kotlin There are several ways to filter a list in kotlin. For example: val  fruits = listOf ("Apple", "Banana", "Orange")                                                               And we will filter values matching "Ap