-
Notifications
You must be signed in to change notification settings - Fork 1
Example of Request pipeline
Kirill edited this page Oct 21, 2020
·
1 revision
public sealed class Request : IRequest<User>
{
public string Name { get; }
public string Email { get; }
public Request(string name, string email)
{
Name = name;
Email = email
}
}
public class PreProcessor : IRequestPreProcessor<Request>
{
public Task Process(Request request, CancellationToken cancellationToken)
{
if (string.IsNullOrWhitespace(request.Name) || string.IsNullOrWhitespace(request.Email))
{
throw new InvalidDataException();
}
}
}
internal sealed class Handler : IRequestHandler<Request, User>
{
private readonly DataContext _context;
public Handler(DataContext context)
{
_context = context;
}
public Task<User> Handle(Request request, CancellationToken cancellationToken)
{
var entry = _context.Users.Add(new User { Name = request.Name, Email = request.Email );
await _context.SaveChangesAsync(cancellationToken);
return entry.Entity;
}
}
internal sealed class PostProcessor : IRequestPostProcessor<Request, User>
{
private readonly IMediator _mediator;
public PostProcessor(IMediator mediator)
{
_mediator = mediator;
}
public Task Process(Request request, User response, CancellationToken cancellationToken)
{
return _mediator.Publish(new UserCreatedNotification(response.Id), cancellationToken);
}
}