1

I am required to learn Dagger 2 for a new project and am struggling a bit to make sense of it all.

I've looked at some Tutorials which give some clarity but I'm still confused by quite a bit, for example how the various moving pieces (Components, Modules, Injectors, Providers) all relate to each other.

Im thinking that perhaps if someone could show me A Dagger equivalent implementation of the code below using Kodein for dependency injection, that would help bridging my gap in understanding:

Injection.kt

fun depInject(app: Application): Kodein {
    return lazy {
        bind<Application>() with instance(app)
        bind<Context>() with instance(app.applicationContext)
        bind<AnotherClass>() with instance(AnotherClass())   
    }
}

BaseApplication.kt

class BaseApplication: Application() {

    companion object {
        lateinit var kodein: Kodein
            private set
    }

    override fun onCreate() {
        super.onCreate()
        kodein = depInject(this)
    }
}

and then anywhere I need to inject I just use:

 private val context: Context by BaseApplication.kodein.instance()

Thanks!

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
Glenncito
  • 902
  • 1
  • 10
  • 23

1 Answers1

1
fun depInject(app: Application): AppComponent {
    return lazy {
        DaggerAppComponent.factory().create(app)
    }
}

and

@Singleton
@Component(modules = [AppModule::class])
interface AppComponent {
    fun context(): Context

    @Component.Factory
    interface Factory {
        fun create(@BindsInstance app: Application): AppComponent
    }
}

@Module
object AppModule {
    @Provides 
    @JvmStatic
    fun context(app: Application): Context = app.applicationContext
}

Then

class BaseApplication: Application() {

    companion object {
        lateinit var component: AppComponent
            private set
    }

    override fun onCreate() {
        super.onCreate()
        component = depInject(this)
    }
}

And

private val context: Context by lazy { BaseApplication.component.context() }

EDIT:

@Singleton class AnotherClass @Inject constructor() {
}

@Singleton
@Component(/*...*/)
interface AppComponent {
    ...

    fun anotherClass(): AnotherClass

    ...
}
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • I have 2 further questions: 1) what would the filename of the code in the 2nd block be called? 2) can you please update to show how it would look if there was the 3rd dependancy i've just added to my example (i.e AnotherClass). – Glenncito Jul 25 '19 at 14:42
  • You're actually still missing an example for binding an implementation to an interface. – EpicPandaForce Jul 25 '19 at 14:47
  • Well your example doesn't have it, so mine doesn't either. :p – EpicPandaForce Jul 25 '19 at 14:51
  • I see :) Well with Kodein I never needed to for it to work as per my needs, but with Dagger it seems its required no? – Glenncito Jul 25 '19 at 14:57
  • Required? No, the translated sample *should* work even without an example for using `@Binds`. ;) – EpicPandaForce Jul 25 '19 at 15:08