Dependency Injection pattern
Most of the classes have some dependency that is needed for the proper functioning of the class. In the general case, we hard-code all the dependencies needed in the class inside the class itself. But this is a very wrong way of providing dependency to a class because in future if the class requires some more dependency or you want to add more dependency, then you have to update the new dependency in all the classes that will be using that dependency. You have to update the code at many places and you should avoid this in your project.
For example?, a car needs an engine to run. So, instead of providing the engine inside the same car class, we should provide the engine from outside the car class.
So, in the Dependency Injection pattern, we provide the dependency of a class from outside the class and no dependency will be provided in the same class?.
For example?, if a class "DataManager" is dependent on "DatabaseHelper" and "NetworkHelper" then we should not provide these two dependencies in the same "DataManager" class because there may be cases when these two dependencies are the dependencies of some other classes also. So, if there is a change in these dependencies, then you have to change the code of these two dependencies in all the classes that are dependent on these two.
Let's understand how to implement Dependency Injection in our code.
If we don't use the concept of Dependency Injection in our code, then our code will look like:
class DataManager {
private val databaseHelper: DatabaseHelper = DatabaseHelper() // dependent on DatabaseHelper
private val networkHelper: NetworkHelper = NetworkHelper() // dependent on NetworkHelper
fun someTask() {
// do some operation with DatabaseHelper and NetworkHelper
}
}
To use the dependency you have to use the below code:
领英推荐
val dataManager: DataManager = DataManager()
dataManager.someTask()
But here, it is very difficult to test each class separately and also if there is a change in dependency, then you have to change the code of the DataManager class again and again.
So, if we provide dependency from outside the class then the code looks something like this:
class DataManager(databaseHelper: DatabaseHelper, networkHelper: NetworkHelper){
private val databaseHelper: DatabaseHelper = databaseHelper
private val networkHelper: NetworkHelper = networkHelper
fun someTask() {
// do some operation with DatabaseHelper and NetworkHelper
}
}
Now, you can use the DatabaseHelper and NetworkHelper by using the below code:
val databaseHelper: DatabaseHelper = DatabaseHelper()
val networkHelper: NetworkHelper = NetworkHelper()
val dataManager: DataManager = DataManager(databaseHelper, networkHelper)
dataManager.someTask()
So by using the DependencyInjection pattern, you will get the ability to test each class in isolation. But every time you want to use the DatabaseHelper and NetworkHelper, you need to write all these codes. So, to make things easier, we make use of a framework called?Dagger?. The Dagger will do all these things for you and make your coding life easier :)
And I will talk about Dagger in the next article
thanks for you @Sumit Mishra
IT SPECIALIST | NETWORK ENGINEER | SYSTEM ADMINISTRATOR | NETWORK SECURITY ENGINEER | SOC ANALYST
1 年Great job bro ??