Closed michaelplavnik closed 10 months ago
Hi @michaelplavnik, thanks for reaching out! It's an interesting idea, I'll definitely play around with it in the next couple of weeks. I'll notify you when something starts to manifest.
This issue is marked stale because it has been open for 90 days with no activity.
Hi @michaelplavnik, I've released 5.13.0-preview-825
with an implementation proposal for this feature.
The configuration option looks like this:
container.Register<Service>(c => c.WithAutoLifetime(Lifetimes.Scoped));
With this, Service
will get the lifetime of its dependencies with the highest lifetime up to the given boundary. For example if all of its dependencies have transient lifetime, it will be transient as well. If one of its dependencies is singleton, it will be scoped because of the Lifetimes.Scoped
boundary.
Let me know what you think!
This feature request is driven by implementing mediator like infrastructure. Most of the time handlers are stateless, and it is up to dependencies to define lifetime. So proposal is to define configuration option LifetimeDescriptor AutoLifeTime that will land Stashbox to derive lifetime from dependencies, but will not be lower then provided bound. For example, for Transient lifetime and AutoLifeTime Scoped it will stop at Scoped not Singleton.