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

About factory provider design, why can not direct set as singleton. #243

Open
llucasspot opened this issue Jul 18, 2024 · 2 comments
Open
Assignees

Comments

@llucasspot
Copy link

Describe the bug / design arrangement
I am reopening this topic : #14

Expected behavior
that factory service can be singleton as well if needed

@llucasspot
Copy link
Author

llucasspot commented Jul 18, 2024

Hi, I see you said years ago :

"This is by design, singleton/transient doesn't really make much sense for a factory. Since with a factory, we want the factory method to run each time we resolve a token."

=> i dont realy understand your words here, why it doesn't really make much sense ?
Sometimes we want to make singleton services where his implementation need a other service from the container

exemple with cart service :

registerByValue(CartApiServiceInterface, new LocalStorageCartApiService());
 registerByFactory(CartService, (container) => {
    return new CartService(container.getInstance(CartApiServiceInterface));
  });

with CartService the fonctional service and CartApiServiceInterface the technical adaptor that can be change if we want to have a api by local storage or web api storage bh exemple

@llucasspot
Copy link
Author

llucasspot commented Jul 18, 2024

that is sad to be force to use instanceCachingFactory to overcome this issue

useFactory: instanceCachingFactory((container) => {
                return useFactory(container);
            }),

this sould be a "buildin" feature like the others with the options LifeCycle

Cordially

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

No branches or pull requests

2 participants