Dagger-Android-Part 1

Dheeraj Andra
MindOrks
Published in
3 min readApr 27, 2019

Welcome to one of the most interesting topic in Android Development, the Dependency Injection framework, Dagger.

Since Dagger is huge and a bit complex topic to understand, we are dividing this series into five parts:

Dagger-Android-Part 1: Deals with Dependency Injection and its role, understanding Dagger as a Dependency Injection framework

Dagger-Android-Part 2: Deals with what is Annotation Processing, what are Qualifiers and scope that are used in Dagger and introduction to custom annotations

Dagger-Android-Part 3: What are Module, Component, Provides and Inject in Dagger

Dagger-Android-Part 4: Getting Started with implementing Dagger in a project

Dagger-Android-Part 5: Completing the project by adding Dagger as a Dependency Injection Framework

So, let’s start with the Part 1 of the series.

What is Dependency Injection(DI) in Software Development ? How can we benefit from Dagger as a DI framework? Well, let’s see..

Dependency Injection is build upon the concept of Inversion of Control. It says that a class should get its dependencies from outside. In simple words, no class should instantiate another class but should get the instances from a configuration class.

The intent behind dependency injection is to decouple objects to the extent that no client code has to be changed simply because an object it depends on needs to be changed to a different one. This permits following the Open / Closed principle

Creating objects directly within the class that requires the objects is inflexible because it commits the class to particular objects and makes it impossible to change(in other words, tightly coupled) the instantiation later independently from (without having to change) the class. It stops the class from being reusable if other objects are required, and it makes the class hard to test because real objects can’t be replaced with mock objects.

An injection, the basic unit of dependency injection, is not a new or a custom mechanism. It works in the same way that “parameter passing” works. Referring to “parameter passing” as an injection carries the added implication that it’s being done to isolate the client from details.

Advantages of DI :

  • Ease of testing is often the first benefit noticed when using dependency injection.
  • Dependency injection allows a client to remove all knowledge of a concrete implementation that it needs to use. This helps isolate the client from the impact of design changes and defects. It promotes reusability, testability and maintainability
  • Reduction of boilerplate code in the application objects, since all work to initialise or set up dependencies is handled by a provider component.
  • Dependency injection allows concurrent or independent development. Two developers can independently develop classes that use each other, while only needing to know the interface the classes will communicate through. Plugins are often developed by third party shops that never even talk to the developers who created the product that uses the plugins.
  • Dependency Injection decreases coupling between a class and its dependency.

So, that’s just a brief on Dependency Injection in software development.

Now, what is Dagger ?

Dagger is a compile time dependency injection framework for both Java and Android. It is an adaptation of an earlier version created by Square and now maintained by Google.

We will be further learning about Dagger in our next parts of the series.

Thats it for Part 1 of this series.

Congratulations on understanding the concept of Dependency Injection and getting introduced to Dagger!

Thank you so much for you time.

Read the next Part(Part 2)of the series here

Let’s become friends on Twitter and LinkedIn

References:

--

--