kosi-libs / Kodein

Painless Kotlin Dependency Injection

Home Page:https://kosi-libs.org/kodein

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Provide a way to delegate to another binding

SalomonBrys opened this issue · comments

This follows a use case defined in #384.

Provided and interface I implemented by a class C, if I want to bind C, and have a binding I that delegates to the C binding, I must write:

bind { singleton { C() } }

bind<I> { provider { instance<C>() } }

This second line is not immediately understandable and requires some mental gymnastic to understand what is a simple delegation.

I propose the following DSL to make this usecase easier to read:

bind { singleton { C() } }

bind<I> { sameAs<C>() }

final syntax is

interface ICache
class Cache : ICache

val di = DI {
    bind { singleton { Cache() } } // Bind concrete type
    delegate<ICache>().to<Cache>() // Delegate binding to concrete bound type
}