var command = new CreateUserCommand(userReq.Email);
var createUserResult = await mediator.Send(command);
My command:
public record CreateUserCommand(string Email) : IRequest<ErrorOr<User>>;
My handler:
public async Task<ErrorOr<User>> Handle(CreateUserCommand command, CancellationToken cancellationToken)
{
var user = await _userRepository.CreateUser(new User(){Email = command.Email});
await _unitOfWork.CommitChangesAsync();
return user;
}
I Register my MediatR like this:
services.AddMediatR(options =>
{
// options.RegisterServicesFromAssemblyContaining(typeof(DependencyInjection));
options.RegisterServicesFromAssembly(typeof(DependencyInjection).Assembly);
});
var command = new CreateUserCommand(userReq.Email);
var createUserResult = await mediator.Send(command);
My command:
public record CreateUserCommand(string Email) : IRequest<ErrorOr<User>>;
My handler:
public async Task<ErrorOr<User>> Handle(CreateUserCommand command, CancellationToken cancellationToken)
{
var user = await _userRepository.CreateUser(new User(){Email = command.Email});
await _unitOfWork.CommitChangesAsync();
return user;
}
I Register my MediatR like this:
services.AddMediatR(options =>
{
// options.RegisterServicesFromAssemblyContaining(typeof(DependencyInjection));
options.RegisterServicesFromAssembly(typeof(DependencyInjection).Assembly);
});