Joan Colmenero
10/18/2019, 2:50 PMbind<MyService>() with singleton {
instance<Retrofit>().create(MyService::class.java)
}
and now I'm wondering if there's a way to avoid doing this
bind<LogniService>() with singleton {
instance<Retrofit>().create(LoginService::class.java)
}
bind<AuthenticationService>() with singleton {
instance<Retrofit>().create(AuthenticationService::class.java)
}
bind<DetailService>() with singleton {
instance<Retrofit>().create(DetailService::class.java)
}
---
streetsofboston
10/18/2019, 2:54 PMRetrofit
instance is used for all of them. Use a KClass<T>
as the multiton’s parameter.Joan Colmenero
10/18/2019, 3:10 PMstreetsofboston
10/18/2019, 3:46 PMinterface RetrofitService // Just an empty marker-interface
....
interface LoginService : RetrofitService { ... }
...
interface AuthenticationService: RetrofitService { ... }
... etc ...
Then define this sub-types binding for your Kodein dependency graph:
...
bind<RetrofitService>().subTypes() with { type ->
singleton {
instance<Retrofit>().create(type.jvmType as Class<RetrofitService>)
}
}
...
And getting/obtaining the correct dependency instance, it is just done the regular way:
...
private val loginService: LoginService by kodein.instance()
...
Joan Colmenero
10/18/2019, 4:11 PM