diff --git a/WarehouseProduct.cs b/WarehouseProduct.cs index 0b1a95c..d1c137e 100644 --- a/WarehouseProduct.cs +++ b/WarehouseProduct.cs @@ -4,16 +4,12 @@ namespace EventSourcing.Demo.Slim { - public class WarehouseProductState - { - public int QuantityOnHand { get; set; } - } - public class WarehouseProduct { private readonly string _sku; private readonly List _uncommittedEvents = new(); - private readonly WarehouseProductState _warehouseProductState = new(); + + private int _quantityOnHand = 0; public WarehouseProduct(string sku) { @@ -22,22 +18,32 @@ public void ShipProduct(int quantity) { - if (quantity > _warehouseProductState.QuantityOnHand) + if (quantity > _quantityOnHand) { throw new InvalidDomainException("Ah... we don't have enough product to ship?"); } AddEvent(new ProductShipped(_sku, quantity, DateTime.UtcNow)); } + + private void Apply(ProductShipped evnt) + { + _quantityOnHand -= evnt.Quantity; + } public void ReceiveProduct(int quantity) { AddEvent(new ProductReceived(_sku, quantity, DateTime.UtcNow)); } + + private void Apply(ProductReceived evnt) + { + _quantityOnHand += evnt.Quantity; + } public void AdjustInventory(int quantity, string reason) { - if (_warehouseProductState.QuantityOnHand + quantity < 0) + if (_quantityOnHand + quantity < 0) { throw new InvalidDomainException("Cannot adjust to a negative quantity on hand."); } @@ -45,19 +51,9 @@ AddEvent(new InventoryAdjusted(_sku, quantity, reason, DateTime.UtcNow)); } - private void Apply(ProductShipped evnt) - { - _warehouseProductState.QuantityOnHand -= evnt.Quantity; - } - - private void Apply(ProductReceived evnt) - { - _warehouseProductState.QuantityOnHand += evnt.Quantity; - } - private void Apply(InventoryAdjusted evnt) { - _warehouseProductState.QuantityOnHand += evnt.Quantity; + _quantityOnHand += evnt.Quantity; } public void ApplyEvent(IEvent evnt)