Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created Jan 5, 2022
Embed
What would you like to do?
namespace SimpleCQRS
{
public class InventoryCommandHandlers
{
private readonly IRepository<InventoryItem> _repository;
public InventoryCommandHandlers(IRepository<InventoryItem> repository)
{
_repository = repository;
}
public void Handle(CreateInventoryItem message)
{
var item = new InventoryItem(message.InventoryItemId, message.Name);
_repository.Save(item, -1);
}
public void Handle(DeactivateInventoryItem message)
{
var item = _repository.GetById(message.InventoryItemId);
item.Deactivate();
_repository.Save(item, message.OriginalVersion);
}
public void Handle(RemoveItemsFromInventory message)
{
var item = _repository.GetById(message.InventoryItemId);
item.Remove(message.Count);
_repository.Save(item, message.OriginalVersion);
}
public void Handle(CheckInItemsToInventory message)
{
var item = _repository.GetById(message.InventoryItemId);
item.CheckIn(message.Count);
_repository.Save(item, message.OriginalVersion);
}
public void Handle(RenameInventoryItem message)
{
var item = _repository.GetById(message.InventoryItemId);
item.ChangeName(message.NewName);
_repository.Save(item, message.OriginalVersion);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment