You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When it comes to validating for lifestyle mismatches, Simple Injector is rather strict. This means that Simple Injector disallows injecting of any dependency with a lifestyle that is shorter than the component it is injected into. Simple Injector considers Transient to be shorter than Scoped, and Scoped to be shorter than Singleton.
Developers, however, sometimes struggle with this behavior, because it also means that a Transient dependency can't be injected into a Scoped component. From a correctness perspective, this makes sense, because a Transient is supposed to be short lived while, theoretically, a scope can live for a very long time. Simple Injector has no knowledge about how long a defined scope will live.
On the other hand, for most application types, the lifetime of a single scope is often well defined and rather short—it is wrapped around a single (web) request. In that scenario it is typically completely safe to inject a Transient dependency into a Scoped consumer.
“Change the lifestyle of the component to a lifestyle that is as short as or shorter than that of the dependency.”
“Change the lifestyle of the dependency to a lifestyle as long as or longer than that of the component.”
Although this advice is sound, there are problem with this:
Upgrading the lifestyle of the dependency from Transient to Scoped is typically not isolated to that single dependency—it has to be done for all the Transient dependencies of the consumer, and snowballs through the graph, because also the dependencies of the dependency need to be (at least) Scoped.
Downgrading the lifestyle of the component from Scoped to Transient can cause another warning: the Disposable Transient Component diagnostic warning. Transients are not disposed in Simple Injector—and upgrading a component (typically a root type that derives from some framework base class that already implements IDisposable) to Scoped effectively fixes this problem.
Because of this, I propose a configuration option that allows loosening the diagnostic lifestyle behavior and allows Transient dependencies to be injected into Scoped components, while still disallowing injecting Transient and Scoped dependencies into Singletons.
Example code:
When it comes to validating for lifestyle mismatches, Simple Injector is rather strict. This means that Simple Injector disallows injecting of any dependency with a lifestyle that is shorter than the component it is injected into. Simple Injector considers
Transient
to be shorter thanScoped
, andScoped
to be shorter thanSingleton
.Developers, however, sometimes struggle with this behavior, because it also means that a
Transient
dependency can't be injected into aScoped
component. From a correctness perspective, this makes sense, because aTransient
is supposed to be short lived while, theoretically, a scope can live for a very long time. Simple Injector has no knowledge about how long a defined scope will live.On the other hand, for most application types, the lifetime of a single scope is often well defined and rather short—it is wrapped around a single (web) request. In that scenario it is typically completely safe to inject a
Transient
dependency into aScoped
consumer.The Simple Injector documentation on lifestyle mismatches currently gives two options in case a mismatch occurs:
Although this advice is sound, there are problem with this:
Transient
toScoped
is typically not isolated to that single dependency—it has to be done for all theTransient
dependencies of the consumer, and snowballs through the graph, because also the dependencies of the dependency need to be (at least)Scoped
.Scoped
toTransient
can cause another warning: the Disposable Transient Component diagnostic warning. Transients are not disposed in Simple Injector—and upgrading a component (typically a root type that derives from some framework base class that already implementsIDisposable
) toScoped
effectively fixes this problem.Because of this, I propose a configuration option that allows loosening the diagnostic lifestyle behavior and allows
Transient
dependencies to be injected intoScoped
components, while still disallowing injectingTransient
andScoped
dependencies intoSingletons
.Example code:
The text was updated successfully, but these errors were encountered: