InsertKoinIO / koin-annotations

Koin Annotations - About Koin - a pragmatic lightweight dependency injection framework for Kotlin & Kotlin Multiplatform insert-koin.io
https://insert-koin.io
Apache License 2.0
123 stars 30 forks source link

Generated modules are global #92

Closed lukelast closed 6 days ago

lukelast commented 9 months ago

Is your feature request related to a problem? Please describe. I'm trying to create many instances of KoinApplication to use in tests that run in parallel. Currently this not possible because the generated module instances are set in global variables.

For example, code like this:

@Module
@ComponentScan("blah")
class ScanModule

Will generate code like this:

public val foo_ScanModule : Module = module {
    single() { Blah() }
}
public val ScanModule.module : org.koin.core.module.Module get() = foo_ScanModule

The foo_ScanModule keeps a single global instance of the module, which causes singletons to be shared among all koin instances created with this module.

Describe the solution you'd like I think calling ScanModule.module should create a new instance of the module each time.

undermark5 commented 8 months ago

Is it ever necessary to reference the module outside of instantiating Koin/multiple times? If so, couldn't this proposal cause problems? I'm not certain that referencing a module multiple times is ever needed, so this may not actually cause any problems.

lukelast commented 8 months ago

I see 2 options for implementing a fix.

  1. Change the existing ScanModule.module to return a new instance of the module each time.
  2. Keep the current behavior, but allow access to get new instances of the module. Like ScanModule.createModule()

Either would work for me. With option 1 you can still create and use a global module that would be shared across multiple koin instances, you would just have to do it explicitly.

arnaudgiuliani commented 8 months ago

Ok interesting 🤔 need to check the global impact of this

stale[bot] commented 3 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

lukelast commented 3 months ago

You're breaking my heart stale bot.

arnaudgiuliani commented 6 days ago

duplicated with #102