What it is and How to do it? – Android Coding by DMTechnolab

Android dependency injection? You may have heard this before. But if you are confused what it is, then this post is for you. Today we will discuss what is dependency injection on Android? Why do we use Dependency Injection? What happens when we do not use dependency injection?

What is dependency injection?

Words “dependency injection” Or briefly “Of” Is free from Android. This is a design pattern in software engineering.
The term is explaining itself as injected dependency.

dependency injectiondependency injection

To understand this in more detail, dependency injections break the term. Dependency and injection into two parts.

What is dependency?

Consider the following Java class.

In the above class, we are making the purpose of the class The database Inside a class constructor the user. So here, we can say that; The user is dependent on the database. So here it is The database Is a dependency for the class the user.

What is an injection?

As we saw the user resting on The database, Now think about how the user can get their dependency, which is database.

We can consider the following ways.

Method # 1

Create a database object inside the user constructor. That is exactly what we are doing in the above code.

This is a really bad idea. We should keep classes independent of other classes. Now you can ask Why?

  • If the classes are independent, you can reuse them.
  • For unit tests for each class, it is necessary that the classes are independent of each other.

Method # 2

Instead of using the new one to accelerate the database inside the user’s constructor, we can pace the database as an argument to the user’s constructor.

So we call it dependency injection. Means supplying dependencies from outside the class. And especially for the above code, we can consider it Constructor Injection.

Now if we want to test the above class then we can pass a dummy object of the class database to the construct.

Android dependency injectionAndroid dependency injection

I hope you have an idea now “What is dependency injection?”.

Definition of dependency injection

I think you got the concept, so here is the definition of dependency injection for you.

Dependency injection in construction on the concept of inversion of control. This means that a class must get its dependency from outside.
In more simple terms, your class cannot immediately send another class using a new keyword inside itself. Instead, you have to supply the object from outside.

But, our main topic was Android Dependency Injection.

By Android dependency injection, here we mean using the same dependency injection pattern when developing Android apps.

Android dependency injection

In this post, we will not see any practical implementation. This was to clear the concept of dependency injection.

So let’s discuss DI in the context of Android Application Development.

Consider a simple application, where we are receiving some data from a web server; Let’s call a list of products (product title, product images, product price, etc.). And then we are displaying the data in our Android application. As we did in it Retrofit android example.

Assuming that we are using retrofit for network request and glide to load images. See the following diagram.

Android dependency injectionAndroid dependency injection

As you can see in the diagram, we have many dependencies. Now our job here is to follow Dependency Injection in the app to make everything easier. And for this, I’m going to use an outline called Dagger 2.

If you are not aware, Dagger 2 is now maintained by Google, and is the most popular dependency injection framework available. Android and Java.

In the next post, we will learn to use Dagger 2 in our Android Studio project.

But remember, Dagger 2 is not the only option available. I will also tell you some other popular Android Dependency Injection Framework used by a lot of developers.

And I will try to tell you a practical implementation about several Android Dependency Injection Framework in my upcoming posts.

Popular Android Dependency Injection Framework

To make life easier, we have some frameworks available for implementing DI.

Dagger 2

Dagger is a fully stable, 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.

Square created this framework, but now Google is maintaining this framework. So I think the best option is to start with Dependency Injection. At the same time, it is easy to learn, and you will find many documents and tutorials available on the Internet.

butter knife

We use this library to inject ideas into your Android components. It is a lightweight library, and it uses annotation processing.
This can save you from using findViewById (). And it will do the ffindViewById () thing automatically for you.

So this post is for friends. And in the next post (Dagger 2 android example), We’ll start with Dagger 2. In the meantime, if you want to ask anything about Android Dependency Injection, leave your comment below. And if you found this post useful, please share it with your friends to help us. Thank you 🙂

Belal Khan ProfileBelal Khan Profile

Hi, my name is Belal Khan and I am Google Developers Specialist (GDE) for Android. A passion for teaching made me this blog. If you are an Android developer, or you are learning about Android development, I can help you a lot with simplified coding.

Source link

About dmtechnolab

Check Also

Simplified Coding

Android Hilt Tutorial – Injecting Dependencies with Hilt – Android Coding by DMTechnolab

You are welcome Android Hilt tutorial. I hope you all are aware of it Dependency …

Leave a Reply

Your email address will not be published. Required fields are marked *