Dagger 2: Difference between revisions

From bibbleWiki
Jump to navigation Jump to search
Line 8: Line 8:
Without Dagger
Without Dagger
<syntaxhighlight lang="kotlin">
<syntaxhighlight lang="kotlin">
fun buildCar: Car =  
fun buildCar: Car =
     Car(SturdyFrame(),
     Car(SturdyFrame(),
     Wheels(),
     Wheels(),
Line 21: Line 21:
     .buildCar()
     .buildCar()
</syntaxhighlight>
</syntaxhighlight>
=Modules=
Modules in Dagger are responsible for providing object we want to inject. They contain the methods which return the objects.
[[File:Modules dagger.png|600px]]

Revision as of 22:59, 19 December 2020

Introduction

Dagger is made by Google. Dagger allows you to

  • Scope dependencies
  • Bind single instance to life cycles
  • Only need to build them once
  • Generates the code at compile time

Example Without Dagger

fun buildCar: Car =  
    Car(SturdyFrame(),
    Wheels(),
    RocketEngine())

With Dagger

fun buildCar: Car = 
    DaggerAppComponent
    .builder()
    .build()
    .buildCar()

Modules

Modules in Dagger are responsible for providing object we want to inject. They contain the methods which return the objects.