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

Optimize RequestHandlerWrapperImpl object construction in Mediator's Send Methods #966

Closed
wants to merge 9 commits into from
8 changes: 3 additions & 5 deletions src/MediatR/Mediator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,14 @@ public Task Send<TRequest>(TRequest request, CancellationToken cancellationToken
throw new ArgumentNullException(nameof(request));
}

var handler = (RequestHandlerWrapper)_requestHandlers.GetOrAdd(request.GetType(), static requestType =>
var handler = (RequestHandlerWrapper) _requestHandlers.GetOrAdd(request.GetType(), static requestType =>
{
var wrapperType = typeof(RequestHandlerWrapperImpl<>).MakeGenericType(requestType);
var wrapper = Activator.CreateInstance(wrapperType) ?? throw new InvalidOperationException($"Could not create wrapper type for {requestType}");
return (RequestHandlerBase)wrapper;
return new RequestHandlerWrapperImpl<TRequest>() ?? throw new InvalidOperationException($"Could not create wrapper type for {typeof(TRequest)}");
});

return handler.Handle(request, _serviceProvider, cancellationToken);
}

public Task<object?> Send(object request, CancellationToken cancellationToken = default)
{
if (request == null)
Expand Down