Instantly share code, notes, and snippets.

Embed
What would you like to do?
public class AddProductCommandHandler : IRequestHandler<AddProductCommand>
{
private readonly ProductDatabase _database;
private readonly IMediator _mediator;
public AddProductCommandHandler(ProductDatabase database, IMediator mediator)
{
_database = database;
_mediator = mediator;
}
public void Handle(AddProductCommand command)
{
// command validation
// add product to database
var @event = new ProductAddedEvent(product.Id, product.Name, product.CategoryId);
_mediator.Publish(@event);
}
}
public class AddReviewCommandHandler : IRequestHandler<AddReviewCommand>
{
private readonly ProductDatabase _database;
private readonly IMediator _mediator;
public AddReviewCommandHandler(ProductDatabase database, IMediator mediator)
{
_database = database;
_mediator = mediator;
}
public void Handle(AddReviewCommand command)
{
// command validation
// add review to database
var @event = new ReviewAddedEvent(review.Id, review.UserId, review.ProductId, review.Rating);
_mediator.Publish(@event);
}
}
public class CompleteOrderCommandHandler : IRequestHandler<CompleteOrderCommand>
{
private readonly ProductDatabase _database;
private readonly IMediator _mediator;
public CompleteOrderCommandHandler(ProductDatabase database, IMediator mediator)
{
_database = database;
_mediator = mediator;
}
public void Handle(CompleteOrderCommand command)
{
// command validation
// create order in database
var @event = new OrderCompletedEvent(order.Id, order.ProductId, order.Amount);
_mediator.Publish(@event);
}
}
public class ChangeFieldValueCommandHandler
{
private readonly ProductDatabase _database;
private readonly IMediator _mediator;
public ChangeFieldValueCommandHandler(ProductDatabase database, IMediator mediator)
{
_database = database;
_mediator = mediator;
}
public void Handle(ChangeFieldValueCommand command)
{
// command validation
// change field value for product
var @event = new FieldValueChangedEvent(fieldValue.Id, fieldValue.Value);
_mediator.Publish(@event);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment