Kotlin sprinkles over Toothpick dependency injection library
define your modules like this:
module {
bind<Repository>(DataBaseRepository::class)
bindInstance<Scheduler>(DefaultScheduler())
bindProvider(ApiProvider::class)
bindProviderInstance(ApiProvider())
bindProviderInstance<Api>{ RestApi() }
}
Instead of
Module().apply {
// bindings
}
use this method with a lambda
module {
// bindings
}
Or if you have a single module, you can directly use the bindings in the scope definition:
simpleScope(SCOPE) {
// bindings
}
or add multiple modules:
scope(SCOPE, {
module {
// bindings
}
module {
// bindings
}
})
Instead of writing
module.bind(Repository::class.java).to(DbRepository::class.java)
Use generics
module.bind<Repository>().to(DbRepository::class.java)
Or write it in one instruction and use the Kotlin KClass
instead of Java Class
type
module.bind<Repository>(DataBaseRepository::class)
Instead of writing
module.bind(Scheduler::class.java).toInstance(DefaultScheduler())
Use generics:
bindInstance<Scheduler>{ DefaultScheduler() }
module.bind(Api::class.java).toProviderInstance(ApiProvider())
use type inference (as provider is already generic):
bindProvider(ApiProvider::class)
bindProviderInstance(ApiProvider())
and you can even directly pass the lambda creating the instance so no need to implement provider
bindProviderInstance<Api>{ RetrofitApi() }
repositories {
maven {url "https://jitpack.io"}
}
compile 'com.github.sporttotal-tv:toothpick-kotlin-extensions:0.2.2'