stephanenicolas / toothpick

A scope tree based Dependency Injection (DI) library for Java / Kotlin / Android.
Apache License 2.0
1.12k stars 115 forks source link

Easy interface binding #429

Open morder opened 3 years ago

morder commented 3 years ago

Dagger is allowing to do smthg like this

interface IFooDi : IFoo, IFooUi

@Binds
@AppScope
abstract fun fooDi(instance: Foo): IFooDi

@Binds
@AppScope
abstract fun foo(instance: IFooDi): IFoo

@Binds
@AppScope
abstract fun fooUi(instance: IFooDi): IFooUi

but with toothpick, you should write a lot of provider classes like

bind(IFooDi::class.java).to(Foo::class.java).singleton()
bind(IFooUi::class.java).toProvider(FooUiProvider::class.java).providesSingleton()
bind(IFoo::class.java).toProvider(FooProvider::class.java).providesSingleton()

class FooUiProvider @Inject constructor(
    private val instance: IFooDi
) : Provider<IFooUi> {
    override fun get() = instance
}

class FooProvider @Inject constructor(
    private val instance: IFooDi
) : Provider<IFoo> {
    override fun get() = instance
}