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

Support @singleton annotation #1522

Open
mikeki opened this issue Aug 8, 2023 · 3 comments
Open

Support @singleton annotation #1522

mikeki opened this issue Aug 8, 2023 · 3 comments

Comments

@mikeki
Copy link

mikeki commented Aug 8, 2023

Expected Behavior

Other IoC libraries have support for the @singleton annotation which indicates that an @injectable class will always resolve inSingletonScope.

I think having support for this annotation would be extremely helpful for my team, and other teams that use inversify.

Current Behavior

Currently it is necessary to create a binding just to mark a class as Singleton, but being singleton is such a core aspect of dependency injection.

@carlossalasamper
Copy link

Hi! If you are using mostly singleton scope dpeendencies in a container you can pass the option defaultScope to the container when you create it:

const container = new Container({ defaultScope: 'Singleton'})

@mikeki
Copy link
Author

mikeki commented Aug 27, 2023

Thank you @carlossalasamper, not all of my injections are singletons, but I was poking around and found https://github.com/inversify/inversify-binding-decorators which allows to create the annotation I needed

IMO we should just get that project as part of the main InversifyJS project too

@notaphplover
Copy link
Member

Hey @mikeki, I'm giving some love to the project these days. I think it's a great idea, I totally agree with you.

I would implement this as an option in the @injectable decorator:

@injectable({
  scope: BindingScope.Singleton,
})
class Foo {}

I'm working on internal refactors and it's my current priority after solving issues, but once I finish it I would love to implement this feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Backlog
Development

No branches or pull requests

3 participants