evant / kotlin-inject

Dependency injection lib for kotlin
Apache License 2.0
1.14k stars 51 forks source link

Support @IntoSet for superclass #368

Open rnett opened 3 months ago

rnett commented 3 months ago

I have something that looks like


interface LifecycleListener {
  fun load() {}
}

interface EntityService {
  fun getEntities(): List<Entity>
}

class DefaultEntityService : EntityService, LifecycleListener  {
  private var entities: List<Entity>? = null

  override fun load() {
    entities = loadEntities() // not shown
  }

  override fun getEntities() {
    return entities
  }

}

to bind this properly, I need to do

interface EntityComponent {
  @get:Provides
  val entityService: EntityService = DefaultEntityService()
  @get:IntoSet
  val DefaultEntityService.bind: LifecycleListener = this
}

which gets even worse when you add scopes.

I would like to be able to do

interface EntityComponent {
  @get:Provides
  @get:IntoSet(LifecycleListener::class)
  val entityService: EntityService = DefaultEntityService()
}
rnett commented 3 months ago

You could support multiple IntoSet annotations on the same binding, too.