koin-annotations icon indicating copy to clipboard operation
koin-annotations copied to clipboard

Support KSP-generated module to be used in multiple KoinApplication instances

Open vdshb opened this issue 1 year ago • 1 comments

I believe KSP should generate:

public val package_name_ModuleName : Module get() = module {
	// ... module internals ...
}

instead of

public val package_name_ModuleName : Module = module {
	// ... module internals ...
}

Currently creating two separated KoinApplications with module generated by KSP in the same runtime gives the same beans instead of different instances.

Example of difference:

val fooModule: Module get() = module { single<Foo>() { Foo() } }
val koinApp1 = koinApplication { modules(fooModule) }
val koinApp2 = koinApplication { modules(fooModule) }
val beanFromKoinApp1 = koinApp1.koin.get<Foo>()
val beanFromKoinApp2 = koinApp1.koin.get<Foo>()
//beanFromKoinApp1 and beanFromKoinApp2 are different instances
val fooModule: Module = module { single<Foo>() { Foo() } }
val koinApp1 = koinApplication { modules(fooModule) }
val koinApp2 = koinApplication { modules(fooModule) }
val beanFromKoinApp1 = koinApp1.koin.get<Foo>()
val beanFromKoinApp2 = koinApp1.koin.get<Foo>()
//beanFromKoinApp1 and beanFromKoinApp2 are the same instance

vdshb avatar Apr 05 '24 11:04 vdshb

Yes, the current implementation is unfortunately, global. See #92 and #102.

Jadarma avatar Apr 30 '24 15:04 Jadarma

Let's continue on those #92 #102. Next milestones should fix that soon

arnaudgiuliani avatar Jun 05 '24 07:06 arnaudgiuliani