z4kn4fein / stashbox

A lightweight, fast, and portable dependency injection framework for .NET-based solutions.
https://z4kn4fein.github.io/stashbox
MIT License
140 stars 10 forks source link

Feature request: automatic upgrade of lifetime #134

Closed michaelplavnik closed 10 months ago

michaelplavnik commented 1 year ago

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.

z4kn4fein commented 1 year 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.

github-actions[bot] commented 1 year ago

This issue is marked stale because it has been open for 90 days with no activity.

z4kn4fein commented 1 year ago

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!