jbogard / MediatR.Extensions.Microsoft.DependencyInjection

MediatR extensions for Microsoft.Extensions.DependencyInjection
MIT License
327 stars 89 forks source link

How can we specify the scope? #44

Closed dimacurotech closed 5 years ago

dimacurotech commented 5 years ago

Currently, AddMediatR uses a "Scoped" scope. How can one use Singleton or Transient scope? Is there a reason to use only Scoped?

jbogard commented 5 years ago

Yes, since this library is mostly intended to be used in ASP.NET Core, the appropriate lifecycle is "scoped". The only two options for ASP.NET Core for me are scoped or transient.

Singleton would carry the most unintended consequences. If you changed anything in this package to use singleton, then no other dependency under anything this resolves could be marked scoped. Singleton is really only appropriate for classes with no other dependencies (since you can't guarantee the lifecycle of dependencies).

Transient is a bit easier, as transients can depend on singleton/scoped items. However, it seems a bit weird to do so.

Where you might want to use singleton are in cases where you're using this package outside of ASP.NET Core. Is that what you're doing?