Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a simpler way to bind items in a set #384

Closed
dalewking opened this issue Oct 5, 2021 · 2 comments
Closed

Create a simpler way to bind items in a set #384

dalewking opened this issue Oct 5, 2021 · 2 comments
Assignees

Comments

@dalewking
Copy link

In our code we create multiple caches and also put them in a set so that we can clear all caches. So I have a function that looks like this:

private fun DI.Builder.bindCaches() {
    bindSet<ICache<*, *>>()

    bind<FooCache> { singleton { Cache(instance()) } }
    inSet<ICache<*, *>> { provider { instance<FooCache>() } }

    bind<BarCache> { singleton { Cache(instance()) } }
    inSet<ICache<*, *>> { provider { instance<BarCache>() } }

    // And so on for many more caches
}

It seems to me that it should be possible to define a DSL to greatly simplify the creation of a binding and adding that to a set. Something like this is what I envision;

private fun DI.Builder.bindCaches() {
    bindSet<ICache<*, *>> {
        bind<FooCache> { singleton { Cache(instance()) } }
        bind<BarCache> { singleton { Cache(instance()) } }
        // And so on for many more caches
   }
}
@SalomonBrys
Copy link
Member

That would be a very good addition.

This DSL feels like a sufficient simplification for what feels the majority of use-cases for us to implement it.

However, it is missing the ability to add in the set without defining a new binding.

How about add instead of bind when adding without binding?

bindSet<ICache> {
    add { singleton { FooCache(instance()) } } // Adds to the set WITHOUT a specific binding
    bind { singleton { BarCache(instance()) } } // Adds to the set WITH a specific binding
}

It is also missing an important ability provided by the original DSL: adding in the set from different modules.
This needs the addition of inBindSet and addInBindSet (formely known as inSet):

val di = DI {
    bindSet<ICache>()

    import(fooModule)
    import(barModule)
}

val fooModule = DI.Module {
    addInBindSet<ICache> { singleton { FooCache() } } // Adds to the set WITHOUT a specific binding
}

val barModule = DI.Module {
    inBindSet<ICache> {
        bind { singleton { BarCache() } } // Adds to the set WITH a specific binding
    }
}

Note that addInBindSet is a syntactic sugar as the fooModule could have been written:

val fooModule = DI.Module {
    inBindSet<ICache> {
        add { singleton { FooCache() } }
    }
}

This therefore deprecates inSet and renames it addInBindSet.
This change should follow the regular depreciation cycle.

@romainbsl
Copy link
Member

It is about time, but it is finally there http://kosi-libs.org/kodein/7.16/core/multi-binding.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants