Dependency Injection: Difference between revisions

From bibbleWiki
Jump to navigation Jump to search
Line 81: Line 81:
So we can now put the theory into practice.Here is our Car class.
So we can now put the theory into practice.Here is our Car class.
<syntaxhighlight lang="kotlin">
<syntaxhighlight lang="kotlin">
class Car(private val engine: Engine, private val wheels: Wheels) {
class Car @Inject  constructor(private var engine: Engine, private var wheels: Wheels) {


    fun drive() {
  private const val TAG = "Car"
        Log.d(TAG, "driving...")
    }


    companion object {
  fun drive() {
        private const val TAG = "Car"
      Log.d(TAG, "driving...")
    }
  }
}
}
</syntaxhighlight>
And the Wheel and Engine
<syntaxhighlight lang="kotlin">
class Wheels @Inject  constructor()
class Engine @Inject  constructor()
</syntaxhighlight>
</syntaxhighlight>
And an interface to allow us to get the Car
And an interface to allow us to get the Car
Line 100: Line 103:
}
}
</syntaxhighlight>
</syntaxhighlight>
Now we can create the Car with
<syntaxhighlight lang="kotlin">
...
    private lateinit var car: Car
...
        val component: CarComponent = DaggerCarComponent.create()
        car = component.getCar()
        car.drive()
</syntaxhighlight>
On Java the interface fails to compile with Missing/Binding however on Kotlin this is allowed. Clearly it would be ideal to identify issues in both.

Revision as of 03:10, 1 February 2021

Introduction

Dependency Injection or DI is when we provides the things we need into another object.

Originally in OO is was thought better to hide the internal objects and create them inside the object.

class Car {
   Engine engine
   Wheels wheels;

   Car() {
      engine = new Engine()
      wheels = new Wheels()
   }

   void drive() {
     // chug chug
   }

}


For Dependency Injection we can provide the prebuilt wheels and engine via the constructor

class Car {
   Engine engine
   Wheels wheels;

   Car(Engine engine, Wheels wheels) {
       this.engine = engine
       this.wheels = wheels
   }

   void drive() {
     // chug chug
   }

}

Why Dagger?

So we taking our example above we can now do.

..
   val engine = new Engine()
   val wheels = new Wheels()
    
   val car = new Car(engine, wheels)
..


Looks simple enough? Well lets add a few more parts

..

   val block = new Block()
   val cylinder = new Cylinder()
   val rims = new Rims()

   val engine = new Engine(block, cylinder)
   val wheels = new Wheels(rims)
    
   val car = new Car(engine, wheels)
..


Dagger exists to help manage the dependencies in terms of

  • dependencies
  • ordering
  • construction

With dagger this becomes

   val carComponent = DaggerCarComponent.create()
   val car = component.getCar()

So here we have a Directed Acyclic Graph' of our car or DAG :)

Initial Project

So we can now put the theory into practice.Here is our Car class.

class Car @Inject  constructor(private var engine: Engine, private var wheels: Wheels) {

   private const val TAG = "Car"

   fun drive() {
       Log.d(TAG, "driving...")
   }
}

And the Wheel and Engine

class Wheels @Inject  constructor()
class Engine @Inject  constructor()

And an interface to allow us to get the Car

@Component
interface CarComponent {

    fun getCar() : Car
}

Now we can create the Car with

...
    private lateinit var car: Car
...

        val component: CarComponent = DaggerCarComponent.create()
        car = component.getCar()
        car.drive()

On Java the interface fails to compile with Missing/Binding however on Kotlin this is allowed. Clearly it would be ideal to identify issues in both.