Skip to content

Commit

Permalink
Upgraded Koin to 4.0.0. Dropped Koin "reverse injection" example per I…
Browse files Browse the repository at this point in the history
  • Loading branch information
ianbrandt committed Oct 4, 2024
1 parent 2581c80 commit 57e58a7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 52 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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<ExternalComponentContainer>(
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<ExternalComponent>(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<ExternalComponentContainer>(
named(
REVERSE_INJECTED_COMPONENT_CONTAINER
)
)

assertThat(externalComponentContainer.externalComponent.value)
.isEqualTo(TESTING)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<GreetingService> { RandomGreetingService() }
single<GreetingService>(named<EnglishGreetingService>()) { EnglishGreetingService() }

single<HelloController> { SimpleHelloController(get()) }
// An unqualified GreetingService
single<GreetingService> {
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<GreetingService>(named<EnglishGreetingService>()) {
EnglishGreetingService()
}

// A parameterized component where the parameter ends up being the component itself
single(named(REVERSE_INJECTED_COMPONENT)) { (externalComponent: ExternalComponent) ->
externalComponent
single<HelloController> {
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))
)
}
}

0 comments on commit 57e58a7

Please sign in to comment.