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

Question about singleton scope in shared container #92

Closed
IvanShah opened this issue Mar 31, 2023 · 5 comments
Closed

Question about singleton scope in shared container #92

IvanShah opened this issue Mar 31, 2023 · 5 comments

Comments

@IvanShah
Copy link

IvanShah commented Mar 31, 2023

Hi there, I have updated on version 2.1.1 and still facing problem from https://github.com/hmlongco/Factory/issues/86 if I have my singleton service in SharedConatiner and it used in other services and containers and it injects somewhere else in my code - service initing twice. First time when it inits for other service, second time when injects. For all my containers I use manager.defaultScope = .singleton. Could you please explain how to fix that?
And I have another question. How can I setup default scope for SharedConatiner services?

@hmlongco
Copy link
Owner

Yep. That's a fairly specific bug in regard to singletons in SharedContainer being resolved across multiple container instances and multiple container classes. I'll look into it.

At this point in time defaultScope is container-based. Any resolution from a container uses that container's default scope.

Your best bet there is simply to mark the service with .singleton.

@IvanShah
Copy link
Author

IvanShah commented Apr 1, 2023

I tried add .singleton to service in SharedContainer but it still inits twice. Behaviour stayed the same.

@hmlongco
Copy link
Owner

hmlongco commented Apr 1, 2023

As mentioned this is a bug.

The "Your best bet there is simply to mark the service with .singleton." is the answer to the second question you asked. Since "SharedContainer" is virtual, there's no associated defaultScope, so each definition would need to marked as singleton.

That approach, however, won't work until the bug is fixed for this specific use case.

@hmlongco
Copy link
Owner

hmlongco commented Apr 1, 2023

It's not an official release yet, but you can pull the main branch and see if this corrects the problem.

@IvanShah
Copy link
Author

IvanShah commented Apr 3, 2023

Yes, it's helped, service init one time thank you

@IvanShah IvanShah closed this as completed Apr 3, 2023
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

2 participants