-
Notifications
You must be signed in to change notification settings - Fork 152
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
How to obtain a lifetime equivalent of DryIoc's ScopedTo<type> #827
Comments
There is no simple way to achieve this with Simple Injector, i.e. there is no built-in feature that you can use to achieve this. There are some extension points though that might allow you to plugin this behavior, but that would take some time for me to figure out how to achieve this. In Simple Injector, the unit for scoping is the What is your specific use case for this? Why doesn't scoping using |
Scoping using However scoping using and outer Currently I am simply gathering info in order to select the most appropriate DI container for our needs. Thanks for taking the time to answer this. |
I decided to reopen your issue, because it allows me to track it as feature request, and, as you stated, it might be a meaningful addition. |
Sorry for intruding. ctx => ctx.OpenScope(
new ScopeName(typeof(Foo)), trackScopeInParent: true)
.Resolve<Foo>(); I imagine you can do this manually if SimpleInjector supports the named / tagged scopes. Update: The important thing to be aware of is tracking the scope in the parent scope or singleton scope. |
Hi @dadhi,
No problem. Thank you for chiming in. |
@dadhi, although I perhaps don't yet see what the use cases are for this feature, I i'm intrigued by this. And questions start to pop up in my mind. For instance, what happens when the 'scoped to' registration ( |
Then it won't be injected (will lead to exception depending on the policy) because the container did not found the scope with the specified name (the name of the service Foo in this example). |
Certain other DI containers provide the notion of a scope that is bound to a specific type instance, such that when the instance is created, all dependencies are resolved in the scope of that instance (provided the dependencies where registered with this type of scoped lifetime).
In some scenarios, a type acts as a natural scope in an application, and in such scenarios this type of scope is useful.
Grabbed and slightly modified from DryIoc docs:
So the main feature here is that the container manages the scope associated with each Foo instance.
Typically you will want a reference to that specific scope, so that it can be disposed along side the instance it's associated with, or if that is not possible, create a scope explicitly and resolve the root from there, and dispose of both together (not the responsibility of the container).
Is there a way to achieve this behaviour in Simple Injector, and if not, would it perhaps be a meaningful addition?
The text was updated successfully, but these errors were encountered: