[Injection] Discriminate default instances during clashes resolution
The idea is to try to resolve the dependency without considering default instances first. This makes containers more composable, e.g., it is now possible to put several containers together as long as all except one provide default implemenetation for some particular service (non-default implementation will be automatically chosen, and all defaults will be discarded).
This commit is contained in:
@@ -33,7 +33,7 @@ enum class ComponentStorageState {
|
||||
Disposed
|
||||
}
|
||||
|
||||
internal class InvalidCardinalityException(message: String, val descriptors: Collection<ComponentDescriptor>) : Exception(message)
|
||||
internal class InvalidCardinalityException(message: String) : Exception(message)
|
||||
|
||||
class ComponentStorage(private val myId: String, parent: ComponentStorage?) : ValueResolver {
|
||||
var state = ComponentStorageState.Initial
|
||||
@@ -46,6 +46,9 @@ class ComponentStorage(private val myId: String, parent: ComponentStorage?) : Va
|
||||
private val dependencies = MultiMap.createLinkedSet<ComponentDescriptor, Type>()
|
||||
|
||||
override fun resolve(request: Type, context: ValueResolveContext): ValueDescriptor? {
|
||||
fun ComponentDescriptor.isDefaultComponent(): Boolean =
|
||||
this is DefaultInstanceComponentDescriptor || this is DefaultSingletonTypeComponentDescriptor
|
||||
|
||||
if (state == ComponentStorageState.Initial)
|
||||
throw ContainerConsistencyException("Container was not composed before resolving")
|
||||
|
||||
@@ -53,9 +56,16 @@ class ComponentStorage(private val myId: String, parent: ComponentStorage?) : Va
|
||||
if (entry.isNotEmpty()) {
|
||||
registerDependency(request, context)
|
||||
|
||||
if (entry.size > 1)
|
||||
throw InvalidCardinalityException("Request $request cannot be satisfied because there is more than one type registered", entry)
|
||||
return entry.singleOrNull()
|
||||
if (entry.size == 1) return entry.single()
|
||||
|
||||
val nonDefault = entry.filterNot { it.isDefaultComponent() }
|
||||
if (nonDefault.isEmpty()) return entry.first()
|
||||
|
||||
return nonDefault.singleOrNull()
|
||||
?: throw InvalidCardinalityException(
|
||||
"Request $request cannot be satisfied because there is more than one type registered\n" +
|
||||
"Clashed registrations: ${entry.joinToString()}"
|
||||
)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
@@ -18,8 +18,16 @@ package org.jetbrains.kotlin.container
|
||||
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
/*Use to assist injection to provide a default implementation for a certain component and reduce boilerplate in injector code.
|
||||
* Argument class must be a non-abstract component class or a kotlin object implementing target interface.
|
||||
* Avoid using when there is no clear 'default' behaviour for a component.
|
||||
* */
|
||||
/**
|
||||
* Use to assist injection to provide a default implementation for a certain component and reduce boilerplate in injector code.
|
||||
* Argument class must be a non-abstract component class or a kotlin object implementing target interface.
|
||||
* Avoid using when there is no clear 'default' behaviour for a component.
|
||||
*
|
||||
* NB: DefaultImplementation are *discriminated* during resolution of components, meaning that:
|
||||
* - if there is exactly one non-default implementation and zero or several default, non-default will be chosen.
|
||||
* - if there is none non-default implementations, default will be chosen
|
||||
*
|
||||
* Such configurations may arise, for example, for multiplatform modules: consider analyzing JVM+JS module, where JS contributes
|
||||
* default implementation of some particular service, and JVM contributes non-default.
|
||||
**/
|
||||
annotation class DefaultImplementation(val impl: KClass<*>)
|
||||
Reference in New Issue
Block a user