Skip to content
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

Add Loosened Lifestyle Mismatch behavior #793

Closed
dotnetjunkie opened this issue Jan 5, 2020 · 1 comment
Closed

Add Loosened Lifestyle Mismatch behavior #793

dotnetjunkie opened this issue Jan 5, 2020 · 1 comment

Comments

@dotnetjunkie
Copy link
Collaborator

dotnetjunkie commented Jan 5, 2020

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.

The Simple Injector documentation on lifestyle mismatches currently gives two options in case a mismatch occurs:

  • “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:

container.Options.UseLoosenedLifestyleMismatchBehavior = true;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant