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

Generated modules are global

Open lukelast opened this issue 2 years ago • 5 comments

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.

lukelast avatar Sep 29 '23 03:09 lukelast

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.

undermark5 avatar Oct 06 '23 15:10 undermark5

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.

lukelast avatar Oct 09 '23 20:10 lukelast

Ok interesting 🤔 need to check the global impact of this

arnaudgiuliani avatar Oct 31 '23 08:10 arnaudgiuliani

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.

stale[bot] avatar Mar 30 '24 05:03 stale[bot]

You're breaking my heart stale bot.

lukelast avatar Apr 02 '24 03:04 lukelast

duplicated with #102

arnaudgiuliani avatar Jun 25 '24 13:06 arnaudgiuliani