i have the problem of injecting different implemen...
# dagger
c
i have the problem of injecting different implementations of an interface depending on a config:
Copy code
@Provides
fun provideManager(config, managerImplA, managerImplB): Manager {
	return if (config.shouldUseA) {
		managerImplA
	} else {
		managerImplB
	}
}
doing this means i have to instantiate both implementation but only one gets used in the end. problem is that all the dependencies of the implementation also will get wired up and kick off some processes that i do not want to be instantiated if that version is not gonna be used. is there a way to achieve this without having the instances already?
f
You can have the config inside your manager impl and decide which gets things running and then provide them like this The other one is to use a provider or lazy to get your manager impl, that way they're not created Other way is to use an init function in order to kick things running so that after your return, you then also init things All of these are just workarounds, you can use a multi binding to provide each manager to a config and get it by the config, of course map<config, lazy<manager>> or a provider instead of lazy, which i think is the most scalable solution out there that won't force you to create such workarounds as aforementioned This way you might have more managers coming out of a config or more X depending on it, that you can obtain really easy, if they're of a same instance you can even inject them into map of config to set and I hope you get the idea of what I'm saying and see how scalable it is
c
thanks for your reply. i think i’d prefer the config not to know about the managers (they are not that tightly coupled) or vice versa. probably i’ll use a provider then. 👍
d
I didn't see this thread and just posted a similar question. Did you ever work out a decent solution?