Dependency Injection pattern

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

Ahmed Abd Elghany

IT SPECIALIST | NETWORK ENGINEER | SYSTEM ADMINISTRATOR | NETWORK SECURITY ENGINEER | SOC ANALYST

1 年

Great job bro ??

要查看或添加评论,请登录

youssef badway的更多文章

  • Network security configuration

    Network security configuration

    The Network Security Configuration in Android is used to customize the behavior of your app's network stack. It allows…

    1 条评论
  • Writing Swift-Friendly Kotlin Multiplatform APIs — Part 1

    Writing Swift-Friendly Kotlin Multiplatform APIs — Part 1

    *This list contains some important notes and solutions for common issues in KMP projects So I will sum up these…

  • Unit Tests

    Unit Tests

    “The Testing Pyramid,” unit tests verify how isolated parts of your application work. Before checking how things work…

    12 条评论
  • Unit tests in TDD part 1

    Unit tests in TDD part 1

    Unit tests Unit tests are the quickest, easiest to write and cheapest to run. They generally test one outcome of one…

    8 条评论
  • Practicing Red-Green-Refactor

    Practicing Red-Green-Refactor

    In this article, you will learn the basics of the TDD process while walking through the Red-Green-Refactor steps…

    2 条评论
  • Compose and the View system can work together side by side.

    Compose and the View system can work together side by side.

    Introduction Compose and the View system can work together side by side. By the end of this article, you'll be able to…

    4 条评论
  • Understand Recomposition

    Understand Recomposition

    jetpack Compose is the newest thing in town. It is still in Alpha, but it shows promising signs.

    4 条评论

社区洞察

其他会员也浏览了