Confused about Scoped registrations in Prism IoC #2655
-
I'm trying to understand scopes in prism Ioc. I have a program that needs to connect to an arbitrary db file and then create a bunch of data services and run some operations. After it's done, I throw them away. I think I would do this with scoped registrations? But I'm struggling to understand how to do that exactly without casting everything to concrete container classes and working with them directly. I looked at the tests to understand scopes via prism IoC and now I'm more confused. In this example below, Prism/tests/Containers/Prism.Container.Shared/Tests/ContainerFixture.cs Lines 355 to 365 in 342689b Can someone clarify how scope/child containers work in Prism Ioc and how I might best go about registering my temporary connection objects? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
After struggling with this a bit more, I'm starting to understand that scoped registrations are different from registering things on a child container. It does not appear Prism has an interface for doing registrations on a child at runtime. Instead I have to grab the underlying container with a cast, create a child, and register my connection object. Was hoping to avoid it so I can port it easier in the future to new versions or different container implementations, but it's not a ton of code in my case so it should be fine. |
Beta Was this translation helpful? Give feedback.
-
Scoping in Prism is only implemented within Prism.Forms. In this case the scope is set to the Page. Each Page has its own scope. For WPF & Uno we do not deal with creating scopes and this becomes the burden of the developer. Prism does not implement registrations on a child container as this goes against our established patterns and recommendations. You should configure the container at application startup / via Prism Modules. containerRegistry.RegisterScoped<IFoo, Foo>(); For context you should consider the following when trying to determine what registration is best.
If you look closely the Container Extension locally stores the latest scope and resolves from it. Additionally you'll notice that we do store off the scope in the PrismNavigationService so that we can attach the scope to the Page. |
Beta Was this translation helpful? Give feedback.
Scoping in Prism is only implemented within Prism.Forms. In this case the scope is set to the Page. Each Page has its own scope. For WPF & Uno we do not deal with creating scopes and this becomes the burden of the developer.
Prism does not implement registrations on a child container as this goes against our established patterns and recommendations. You should configure the container at application startup / via Prism Modules.
For context you should consider the following when trying to determine what registration is best.