inversify / InversifyJS

A powerful and lightweight inversion of control container for JavaScript & Node.js apps powered by TypeScript.
http://inversify.io/
MIT License
11.02k stars 712 forks source link

Support @singleton annotation #1522

Open mikeki opened 11 months ago

mikeki commented 11 months ago

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 commented 11 months ago

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 commented 11 months ago

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