I have almost the same question than Kevin I'm usi...
# kodein
j
I have almost the same question than Kevin I'm using this for my retrofit instance
Copy code
bind<MyService>() with singleton {
        instance<Retrofit>().create(MyService::class.java)
    }
and now I'm wondering if there's a way to avoid doing this
Copy code
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)
    }
---
s
i don’t see why it is bad practice. You could also create a multiton (instead of a singleton) binding, if the same
Retrofit
instance is used for all of them. Use a
KClass<T>
as the multiton’s parameter.
j
can you post a sample?
s
First, have all your retrofit-service interfaces extend a super-interface. e.g.
Copy code
interface RetrofitService // Just an empty marker-interface
....
interface LoginService : RetrofitService { ... }
...
interface AuthenticationService: RetrofitService { ... }
... etc ...
Then define this sub-types binding for your Kodein dependency graph:
Copy code
...
    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:
Copy code
...
    private val loginService: LoginService by kodein.instance()
    ...
j
Cool let me try it 🙂