Endre Deak
01/09/2024, 6:03 PMimport org.koin.dsl.koinApplication
import org.koin.dsl.module
import org.koin.core.module.Module
class Foo
fun main() {
val module = module { single<Foo> { Foo() } }
val moduleFunc = { module { single<Foo> { Foo() } } }
check(module, module)
check(module, moduleFunc())
}
fun check(module1: Module, module2: Module) {
val app1 = koinApplication { modules(module1) }
val foo1 = app1.koin.get<Foo>()
val app2 = koinApplication { modules(module2) }
val foo2 = app2.koin.get<Foo>()
if (app1.koin == app2.koin) {
error("koin is the same")
}
if (foo1 == foo2) {
println("same")
} else {
println("different")
}
}
// output:
same
different
My understanding was that a module is defining the how, not the what, and because of that, if I pass the same module to two different koin applications, I'd expect to create a different instance for single
, but it's not the case. How could I achieve that without creating a new module for each koinApplication
?Pedro Alberto
01/09/2024, 7:28 PMEndre Deak
01/09/2024, 8:00 PMEndre Deak
01/09/2024, 8:00 PMPedro Alberto
01/10/2024, 9:45 AMPedro Alberto
01/10/2024, 9:48 AMEndre Deak
01/10/2024, 3:44 PMdifferent
different
so if I inject the same module to two applications, the created instances are different. According to the test, in order to have that behaviour, one has to create a new instance of the module per koin application.arnaud.giuliani
01/11/2024, 2:44 PMarnaud.giuliani
01/11/2024, 2:44 PMFoo
Endre Deak
01/11/2024, 5:22 PMsame
different
I only get different instances of Foo
when the modules themselves are differentarnaud.giuliani
01/11/2024, 5:44 PMarnaud.giuliani
01/11/2024, 5:44 PMarnaud.giuliani
01/11/2024, 5:45 PMarnaud.giuliani
01/11/2024, 5:45 PMEndre Deak
01/11/2024, 7:15 PMEndre Deak
01/11/2024, 7:16 PMarnaud.giuliani
01/12/2024, 8:12 AM