Skip to content

Commit 07bc997

Browse files
authored
fix((#504): bring back scope on SetBinder and ArgSetBinder (#505)
1 parent a50b4e9 commit 07bc997

3 files changed

Lines changed: 74 additions & 10 deletions

File tree

kodein-di/src/commonMain/kotlin/org/kodein/di/DI.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,7 @@ public interface DI : DIAware {
304304
/**
305305
* Manage multiple bindings in a [Set]
306306
*/
307-
@DIDsl
308-
public interface SetBinder<T : Any> : BindBuilder.WithScope<Any> {
307+
public interface SetBinder<T : Any> {
309308

310309
/**
311310
* Add a binding in the [Set] of type [T]
@@ -379,8 +378,7 @@ public interface DI : DIAware {
379378
/**
380379
* Manage multiple bindings, with type argument, in a [Set]
381380
*/
382-
@DIDsl
383-
public interface ArgSetBinder<A : Any, T : Any> : BindBuilder.WithScope<Any> {
381+
public interface ArgSetBinder<A : Any, T : Any> {
384382

385383
/**
386384
* Add a binding in the [Set] of type [T]

kodein-di/src/commonMain/kotlin/org/kodein/di/internal/DIBuilderImpl.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ internal open class DIBuilderImpl internal constructor(
8787
addSetBindingToContainer: Boolean = true,
8888
) : DI.Builder.SetBinder<T> {
8989

90-
override val contextType: TypeToken<Any> get() = this@DIBuilderImpl.contextType
91-
override val scope: Scope<Any?> get() = this@DIBuilderImpl.scope
92-
override val explicitContext: Boolean get() = this@DIBuilderImpl.explicitContext
90+
val contextType: TypeToken<Any> get() = this@DIBuilderImpl.contextType
91+
val scope: Scope<Any?> get() = this@DIBuilderImpl.scope
92+
val explicitContext: Boolean get() = this@DIBuilderImpl.explicitContext
9393

9494
private val setBinding: BaseMultiBinding<*, *, T> by lazy {
9595
val setType = erasedComp(Set::class, setBindingType) as TypeToken<Set<T>>
@@ -161,9 +161,9 @@ internal open class DIBuilderImpl internal constructor(
161161
addSetBindingToContainer: Boolean = true,
162162
) : DI.Builder.ArgSetBinder<A, T> {
163163

164-
override val contextType: TypeToken<Any> get() = this@DIBuilderImpl.contextType
165-
override val scope: Scope<Any?> get() = this@DIBuilderImpl.scope
166-
override val explicitContext: Boolean get() = this@DIBuilderImpl.explicitContext
164+
val contextType: TypeToken<Any> get() = this@DIBuilderImpl.contextType
165+
val scope: Scope<Any?> get() = this@DIBuilderImpl.scope
166+
val explicitContext: Boolean get() = this@DIBuilderImpl.explicitContext
167167

168168
private val setBinding: BaseMultiBinding<*, in A, out T> by lazy {
169169
val setType = erasedComp(Set::class, setBindingType) as TypeToken<Set<T>>

kodein-di/src/commonTest/kotlin/org/kodein/di/Tests_13_Scope.kt

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,4 +440,70 @@ class Tests_13_Scope {
440440
assertTrue(c.closed)
441441
}
442442

443+
@Test
444+
fun test_18_ScopedSetBindingSingleton() {
445+
val registries = mapOf("a" to SingleItemScopeRegistry(), "b" to SingleItemScopeRegistry())
446+
val myScope = object : Scope<String> {
447+
override fun getRegistry(context: String) = registries[context]!!
448+
}
449+
450+
val di = DI {
451+
bindSet<IPerson> {
452+
add { scoped(myScope).singleton { Person("Salomon") } }
453+
add { scoped(myScope).singleton { Person("Laila") } }
454+
}
455+
}
456+
457+
val personsA: Set<IPerson> by di.on(context = "a").instance()
458+
val personsB: Set<IPerson> by di.on(context = "b").instance()
459+
460+
assertEquals(2, personsA.size)
461+
assertEquals(2, personsB.size)
462+
463+
val personsA2: Set<IPerson> by di.on(context = "a").instance()
464+
assertEquals(personsA, personsA2)
465+
466+
registries["a"]!!.clear()
467+
val personsA3: Set<IPerson> by di.on(context = "a").instance()
468+
assertTrue(personsA.none { a -> personsA3.any { b -> a === b } })
469+
}
470+
471+
@Test
472+
fun test_19_NonScopedSetBindingConvenienceMethods() {
473+
val di = DI {
474+
bindSet<IPerson> {
475+
addSingleton { Person("Salomon") }
476+
addProvider { Person("Laila") }
477+
addInstance(Person("Mary"))
478+
}
479+
}
480+
481+
val persons: Set<IPerson> by di.instance()
482+
assertEquals(3, persons.size)
483+
assertTrue(Person("Salomon") in persons)
484+
assertTrue(Person("Laila") in persons)
485+
assertTrue(Person("Mary") in persons)
486+
}
487+
488+
@Test
489+
fun test_20_ScopedArgSetBindingMultiton() {
490+
val registries = mapOf("a" to SingleItemScopeRegistry(), "b" to SingleItemScopeRegistry())
491+
val myScope = object : Scope<String> {
492+
override fun getRegistry(context: String) = registries[context]!!
493+
}
494+
495+
val di = DI {
496+
bindArgSet<String, IPerson> {
497+
add { scoped(myScope).multiton { name: String -> Person(name) } }
498+
}
499+
}
500+
501+
val personsA: Set<IPerson> by di.on(context = "a").instance(arg = "Salomon")
502+
assertEquals(1, personsA.size)
503+
assertTrue(Person("Salomon") in personsA)
504+
505+
val personsA2: Set<IPerson> by di.on(context = "a").instance(arg = "Salomon")
506+
assertEquals(personsA, personsA2)
507+
}
508+
443509
}

0 commit comments

Comments
 (0)