Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created January 5, 2022 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcomartin/f51114d6e1352710e8aca2d85720e0ca to your computer and use it in GitHub Desktop.
Save dcomartin/f51114d6e1352710e8aca2d85720e0ca to your computer and use it in GitHub Desktop.
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