diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 18f6c5a..6050140 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -8,7 +8,7 @@ equalsverifier = "3.17.1" jetbrains-annotations = "25.0.0" junit = "5.11.1" jvm-dependency-conflict-resolution = "2.1.2" -koin = "3.5.6" +koin = "4.0.0" kotest = "5.9.1" kotlin = "2.0.20" # Must match KSP version below. kotlinpoet = "1.18.1" diff --git a/subprojects/di-with-koin/src/it/kotlin/org/sdkotlin/koin/it/hello/HelloModuleIT.kt b/subprojects/di-with-koin/src/it/kotlin/org/sdkotlin/koin/it/hello/HelloModuleIT.kt index 8ae654a..85fe75e 100644 --- a/subprojects/di-with-koin/src/it/kotlin/org/sdkotlin/koin/it/hello/HelloModuleIT.kt +++ b/subprojects/di-with-koin/src/it/kotlin/org/sdkotlin/koin/it/hello/HelloModuleIT.kt @@ -5,7 +5,6 @@ import org.assertj.core.api.Assertions.assertThatExceptionOfType import org.junit.jupiter.api.Test import org.junit.jupiter.api.extension.RegisterExtension import org.koin.core.error.InstanceCreationException -import org.koin.core.parameter.parametersOf import org.koin.core.qualifier.named import org.koin.test.KoinTest import org.koin.test.get @@ -16,8 +15,6 @@ import org.sdkotlin.koin.hello.DECLARED_COMPONENT_CONTAINER import org.sdkotlin.koin.hello.ExternalComponent import org.sdkotlin.koin.hello.ExternalComponentContainer import org.sdkotlin.koin.hello.HelloController -import org.sdkotlin.koin.hello.REVERSE_INJECTED_COMPONENT -import org.sdkotlin.koin.hello.REVERSE_INJECTED_COMPONENT_CONTAINER import org.sdkotlin.koin.hello.RandomGreetingService import org.sdkotlin.koin.hello.helloModule @@ -58,35 +55,4 @@ internal class HelloModuleIT : KoinTest { assertThat(externalComponentContainer.externalComponent.value) .isEqualTo(TESTING) } - - @Test - fun `test reverse injecting an external component`() { - - assertThatExceptionOfType(InstanceCreationException::class.java) - .isThrownBy { - get( - named( - REVERSE_INJECTED_COMPONENT_CONTAINER - ) - ) - } - - // "Reverse inject" the external component into the Koin module as an - // injection parameter for a singleton that is the parameter itself - get(named(REVERSE_INJECTED_COMPONENT)) { - parametersOf(ExternalComponent(TESTING)) - } - - // Get another component from Koin that depends on the external - // component having been injected into the module - val externalComponentContainer = - get( - named( - REVERSE_INJECTED_COMPONENT_CONTAINER - ) - ) - - assertThat(externalComponentContainer.externalComponent.value) - .isEqualTo(TESTING) - } } diff --git a/subprojects/di-with-koin/src/main/kotlin/org/sdkotlin/koin/hello/helloModule.kt b/subprojects/di-with-koin/src/main/kotlin/org/sdkotlin/koin/hello/helloModule.kt index aebc97e..3e6f872 100644 --- a/subprojects/di-with-koin/src/main/kotlin/org/sdkotlin/koin/hello/helloModule.kt +++ b/subprojects/di-with-koin/src/main/kotlin/org/sdkotlin/koin/hello/helloModule.kt @@ -5,32 +5,28 @@ import org.koin.dsl.module const val DECLARED_COMPONENT = "declared component" const val DECLARED_COMPONENT_CONTAINER = "declared component container" -const val REVERSE_INJECTED_COMPONENT = "reverse injected component" -const val REVERSE_INJECTED_COMPONENT_CONTAINER = "reverse injected component container" internal val helloModule = module { - single { RandomGreetingService() } - single(named()) { EnglishGreetingService() } - - single { SimpleHelloController(get()) } + // An unqualified GreetingService + single { + RandomGreetingService() + } - // Injection will fail if DECLARED_COMPONENT isn't externally declared prior, e.g. with getKoin().declare(...) - single(named(DECLARED_COMPONENT_CONTAINER)) { - ExternalComponentContainer( - get(named(DECLARED_COMPONENT)) - ) + // A named GreetingService + single(named()) { + EnglishGreetingService() } - // A parameterized component where the parameter ends up being the component itself - single(named(REVERSE_INJECTED_COMPONENT)) { (externalComponent: ExternalComponent) -> - externalComponent + single { + SimpleHelloController(get()) } - // Injection will fail if REVERSE_INJECTED_COMPONENT isn't reverse injected prior - single(named(REVERSE_INJECTED_COMPONENT_CONTAINER)) { + // Injection will fail if DECLARED_COMPONENT isn't externally declared + // prior, e.g. with getKoin().declare(...) + single(named(DECLARED_COMPONENT_CONTAINER)) { ExternalComponentContainer( - get(named(REVERSE_INJECTED_COMPONENT)) + get(named(DECLARED_COMPONENT)) ) } }