Guidance for managing service lifetimes across different parts of your application #658
Replies: 2 comments 1 reply
-
Did you try to use |
Beta Was this translation helpful? Give feedback.
-
Hi @dadhi Could you please provide further guidance on how to use this? I tried to understand the documentation, but I’m having trouble grasping this new and bit advanced concept. By the way one important bit that it's not RuleApp who need to use IExpressionParser alone but every part of the application will require IExpressionParser until the RuleApp is not changed by the RuleAppService. Further, any comment about the approach I mentioned in the question? |
Beta Was this translation helpful? Give feedback.
-
I'm working on a project where I need to register a service (IExpressionParser) using DryIoc. The service should behave like a singleton for the lifetime of a RuleApp instance, but different RuleApp instances should have their own independent instances of IExpressionParser.
I tried using Reuse.Scoped to manage this, but parts of my code do not use scopes when resolving dependencies via constructor injection. As a result, I'm seeing multiple instances of IExpressionParser being created in different parts of the code, even during the lifetime of the same RuleApp.
Here’s what I’ve tried so far:
Registering IExpressionParser with Reuse.Scoped and opening a scope for RuleApp.
Directly registering it as a singleton using Reuse.Singleton and resolving it manually, but this does not isolate instances per RuleApp.
What would be the best approach to ensure that:
IExpressionParser is created once per RuleApp instance and shared across all dependencies within that RuleApp.
Code that does not explicitly use scopes can still resolve the correct IExpressionParser for the current RuleApp.
Is there a pattern or feature in DryIoc that allows managing service lifetimes like this, where instances are scoped per logical context (RuleApp), without requiring explicit scope management in all parts of the code?
One approach I have taken, not sure if this is an elegant and optimized way to handle this problem.
Beta Was this translation helpful? Give feedback.
All reactions