Skip to content

Commit

Permalink
Merge pull request #844 from remcoros/patch-2
Browse files Browse the repository at this point in the history
Use actual request type in new 'Send' method
  • Loading branch information
jbogard authored Feb 15, 2023
2 parents a9672fb + 395ef0f commit 59c9727
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/MediatR/Mediator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public Task Send<TRequest>(TRequest request, CancellationToken cancellationToken
throw new ArgumentNullException(nameof(request));
}

var requestType = typeof(TRequest);
var requestType = request.GetType();

var handler = (RequestHandlerWrapper)_requestHandlers.GetOrAdd(requestType,
static t => (RequestHandlerBase)(Activator.CreateInstance(typeof(RequestHandlerWrapperImpl<>).MakeGenericType(t))
Expand Down Expand Up @@ -207,4 +207,4 @@ public IAsyncEnumerable<TResponse> CreateStream<TResponse>(IStreamRequest<TRespo

return items;
}
}
}
5 changes: 3 additions & 2 deletions test/MediatR.Tests/PublishTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,9 @@ public async Task Should_resolve_handlers_given_interface()

var mediator = container.GetInstance<IMediator>();

INotification notification = new Ping { Message = "Ping" };
await mediator.Publish(notification);
// wrap notifications in an array, so this test won't break on a 'replace with var' refactoring
var notifications = new INotification[] { new Ping { Message = "Ping" } };
await mediator.Publish(notifications[0]);

var result = builder.ToString().Split(new[] { Environment.NewLine }, StringSplitOptions.None);
result.ShouldContain("Ping Pong");
Expand Down
25 changes: 25 additions & 0 deletions test/MediatR.Tests/SendTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,31 @@ public async Task Should_resolve_main_handler_by_specific_interface()
response.Message.ShouldBe("Ping Pong");
}

[Fact]
public async Task Should_resolve_main_handler_by_given_interface()
{
var dependency = new Dependency();
var container = new Container(cfg =>
{
cfg.Scan(scanner =>
{
scanner.AssemblyContainingType(typeof(PublishTests));
scanner.IncludeNamespaceContainingType<VoidPing>();
scanner.WithDefaultConventions();
scanner.AddAllTypesOf(typeof(IRequestHandler<>));
});
cfg.ForSingletonOf<Dependency>().Use(dependency);
cfg.For<ISender>().Use<Mediator>();
});

var mediator = container.GetInstance<ISender>();

// wrap requests in an array, so this test won't break on a 'replace with var' refactoring
var requests = new IRequest[] { new VoidPing() };
await mediator.Send(requests[0]);

dependency.Called.ShouldBeTrue();
}

[Fact]
public async Task Should_raise_execption_on_null_request()
Expand Down

0 comments on commit 59c9727

Please sign in to comment.