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

[feature] lazy singleton #1463

Open
xenoterracide opened this issue May 27, 2022 · 1 comment
Open

[feature] lazy singleton #1463

xenoterracide opened this issue May 27, 2022 · 1 comment

Comments

@xenoterracide
Copy link

xenoterracide commented May 27, 2022

I'd like a way to use a function to create a singleton. From what I understand from the documentation factories and providers return an instance, but said instance itself is not a singleton unless you do it in another way. Something like

container.bind('foo').creator(context => any).inSingletonScope()

would be super useful as sometimes there's stuff you want from the container in a function to create the object.

Obviously you can do some complex logic to do this in a factory, where you check if the value exists in the container, or bind it.

every DI system I've used can do this except, from what I can tell, this one.

@dokmic
Copy link

dokmic commented Aug 15, 2022

Hey @xenoterracide,

It was always possible with the .toDynamicValue() binding. The code should look something like that:

container.bind('foo').toDynamicValue(context => 'bar').inSingletonScope()

The foo service will be lazily initialized upon request.

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

No branches or pull requests

2 participants