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

Add scoped lifetime to DI - inject the same service instance to child components #16060

Closed
Andrzej-W opened this issue May 4, 2018 · 1 comment
Labels
area-blazor Includes: Blazor, Razor Components

Comments

@Andrzej-W
Copy link

Let's assume I have a Customer component - this is the parent component. I also have CustomerList and CustomerDetails components - these are child components and they are displayed inside Customer component. All three components have to share some data and some business logic including Web API calls. I want to encapsulate this in CustomerService. Currently the only reasonable choice is to register CustomerService as Singleton and then inject it into all three Customer* components.

It's OK but below is another scenario where Singleton is not enough.

I have CustomerComparer component and I want to display two different customers side by side. To do this I want to use two Customer components. Of course they cannot work with the same instance of CustomerService. The only solution I see here is to register CustomerService as Scoped (which is currently equivalent to Singleton). Scoped should work as follows:

  • service injected into the first component in hierarchy should work as Transient,
  • all child components should get the same service instance as parent component.

With the above functionality I will be able to display two Customer components at the same time.

Look at #5496 also.

@SteveSandersonMS
Copy link
Member

We would probably handle this by a mechanism like context in #5455 rather than DI. There are often situations where you'd want to share state with a subtree without having to register things in DI, and once we enable that, you wouldn't need the DI approach. Hope that makes sense!

@mkArtakMSFT mkArtakMSFT transferred this issue from dotnet/blazor Oct 27, 2019
@mkArtakMSFT mkArtakMSFT added the area-blazor Includes: Blazor, Razor Components label Oct 27, 2019
@ghost ghost locked as resolved and limited conversation to collaborators Dec 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components
Projects
None yet
Development

No branches or pull requests

3 participants