Skip to content

Instantly share code, notes, and snippets.

@ZacSweers
Last active October 2, 2017 14:26
Show Gist options
  • Save ZacSweers/d0841dd785d913103d43e548326740f2 to your computer and use it in GitHub Desktop.
Save ZacSweers/d0841dd785d913103d43e548326740f2 to your computer and use it in GitHub Desktop.
Reusable injections
class Parent {
// Somewhere in here, a lot of external service modules are included and contribute their `Service` impls to this multibinding
@Inject lateinit var services: Map<String, Provider<Service>>
fun showChild() {
addChildController(serviceKey = "barServiceKey") // Key into the services map
}
}
// Child class could be some presentation UI, reusable and just shows data based on a given service
class Child {
// Currently, I have to inject this services map as a whole
// I'd like to get this down to something that only exposes the Service for better isolation
@Inject lateinit var services: Map<String, Provider<Service>>
lateinit var actualService: Service
// So what I'd like to do is something that lets me swizzle this down to be able to do this based on what's found at compile time
// @Inject lateinit var service: Service
fun performInjection() {
// do injection...
actualService = services[getArguments()["serviceKey"]].get()
}
fun showUi() {
with(actualService) {
// Show data
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment