Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn for mixing singleton with non-singleton scopes #1557

Open
dstoyanoff opened this issue Feb 12, 2024 · 0 comments
Open

Warn for mixing singleton with non-singleton scopes #1557

dstoyanoff opened this issue Feb 12, 2024 · 0 comments

Comments

@dstoyanoff
Copy link

I just resolved a very nasty bug I had in my app, which could have been prevented if inversify had a warning about this use case.
I basically had a service, registered as singleton, but it had, by mistake, dependencies that are scoped. By doing that, the dependencies are actually becoming singletons as they are only initialized when the singleton is instantiated, which could potentially cause major issues

Expected Behavior

Inversify should print a warning if scoped/transient dependency is used in a singleton

Current Behavior

Inversify lets you use the scoped service in a singleton, causing wrong instances.

Possible Solution

Coming from .NET in the past, with the built-in DI container, the framework would force that any dependencies are in scope order -> transient can depend on scoped (which is thread-local there) or singleton, scoped can depend on transient, but the opposite order is forbidden and it would throw an error when registered.

Your Environment

  • Version used: 6.0.1
  • Environment name and version (e.g. Chrome 39, node.js 5.4): Node 18.18.2
  • Operating System and version (desktop or mobile): macOS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant