jbogard / MediatR

Simple, unambitious mediator implementation in .NET
Apache License 2.0
10.91k stars 1.16k forks source link

Consider adding new overload to AddOpenBehavior #934

Closed jonathanhallee closed 8 months ago

jonathanhallee commented 1 year ago

Hey,

We are currently in the process of migrating to Mediatr v12.1 and changing our old behavior registrations from generic AddScoped registrations to using the provided AddOpenBehavior extensions.

One thing we find missing is an overload of AddOpenBehavior that accepts a Func<IServiceDescriptor, object> which would allow us to more easily migrate funky registrations where we do constructor injection with a func.

Example:

services.AddScoped((Func<IServiceProvider, IPipelineBehavior<TRequest, TResponse>>)((IServiceProvider x) => new SomeBehavior<TRequest, TResponse>(someParameter)));

would be translated to something like

config.AddOpenBehavior(typeof(SomeBehavior<,>), (IServiceProvider x) => new SomeBehavior<TRequest, TResponse>(someParameter)))

The ServiceDescriptor class already accepts it

I don't see anything else allowing us to do this?

Thanks!

jbogard commented 11 months ago

I'll take a PR!

jscarle commented 11 months ago

I looked into this, its does not seem possible to do this with the way pipelines are currently registered. The Dependency Injection container doesn't support registering open generics with a factory directly. It throws an exception:

"Open generic service type 'MediatR.IPipelineBehavior`2[TRequest,TResponse]' requires registering an open generic implementation type. (Parameter 'descriptors')".
github-actions[bot] commented 9 months ago

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 14 days.

github-actions[bot] commented 8 months ago

This issue was closed because it has been stalled for 14 days with no activity.