How to instantiate Mediatr as part of a Unit Test?

You’re on the right lines with or a mockup – you need to mock the IMediator There’s a few mocking libraries out there: Moq FakeItEasy NSubstitute Moq is one of the most popular, so, using your test as an example: [Fact] public async void UpdateCustomerCommand_CustomerDataUpdatedOnDatabase() { //Arrange var mediator = new Mock<IMediator>(); UpdateCustomerCommand command = … Read more

MediatR publish and MediatR send

MediatR has two kinds of messages it dispatches: Request/response messages, dispatched to a single handler Notification messages, dispatched to multiple handlers Send may return a response, but do not have to do it. Publish never return the result. You are sending requests (sometimes called commands) by _mediator.Send({command}) to exactly one concrete handler. It may be … Read more

Add validation to a MediatR behavior pipeline?

The process is exactly the same, you just have to change the interface to use the new IPipelineBehavior<TRequest, TResponse> interface. public class ValidationBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> where TRequest : IRequest<TResponse> { private readonly IEnumerable<IValidator<TRequest>> _validators; public ValidationBehavior(IEnumerable<IValidator<TRequest>> validators) { _validators = validators; } public Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next) { var context = new … Read more

MediatR with ASP.NET Core DI

For the latest MediatR version 12.x February 2023: As of version 12, Mediatr offers support for registering Handlers with the Microsoft Dependency Injection framework directly within the Mediatr namespace, and so can be achieved when calling AddMediatR on the Service collection. services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(typeof(Ping).Assembly)); Where Ping is a type that is contained in an assembly … Read more

ASP.NET Core MediatR error: Register your handlers with the container

I have met the same issue. The problem is that this line code services.AddMediatR(typeof(AddEducationCommand).GetTypeInfo().Assembly); handles all the MediatR IRequest and IRequestHandlers. but you created an IRepository interface and its implementation class which can’t be handled by that MediatR.Extensions.Microsoft.DependencyInjection so keep all your changes but add this – manually register this like services.AddScoped(typeof(IUniversityRepository), typeof(UniversitySqlServerRepository)); then issue … Read more

IRequestHandler return void

Generally speaking, if a Task based method does not return anything you can return a completed Task public Task Handle(CreatePersonCommand message, CancellationToken cancellationToken) { return Task.CompletedTask; } Now, in MediatR terms a value needs te be returned. In case of no value you can use Unit: public Task<Unit> Handle(CreatePersonCommand message, CancellationToken cancellationToken) { return Task.FromResult(Unit.Value); … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)