-
-
Save dcomartin/e97f692f81e04a5e9be098f439e16b8a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace EventSourcing.Demo | |
{ | |
public class WarehouseProductState | |
{ | |
public int QuantityOnHand { get; set; } | |
} | |
public class WarehouseProduct : AggregateRoot | |
{ | |
public string Sku { get; } | |
private readonly WarehouseProductState _warehouseProductState = new(); | |
public WarehouseProduct(string sku) | |
{ | |
Sku = sku; | |
} | |
public override void Load(IEnumerable<IEvent> events) | |
{ | |
foreach (var evnt in events) | |
{ | |
Apply(evnt as dynamic); | |
} | |
} | |
public static WarehouseProduct Load(string sku, IEnumerable<IEvent> events) | |
{ | |
var warehouseProduct = new WarehouseProduct(sku); | |
warehouseProduct.Load(events); | |
return warehouseProduct; | |
} | |
public void ShipProduct(int quantity) | |
{ | |
if (quantity > _warehouseProductState.QuantityOnHand) | |
{ | |
throw new InvalidDomainException("Cannot Ship to a negative Quantity on Hand."); | |
} | |
var productShipped = new ProductShipped(Sku, quantity, DateTime.UtcNow); | |
Apply(productShipped); | |
Add(productShipped); | |
} | |
private void Apply(ProductShipped evnt) | |
{ | |
_warehouseProductState.QuantityOnHand -= evnt.Quantity; | |
} | |
public void ReceiveProduct(int quantity) | |
{ | |
var productReceived = new ProductReceived(Sku, quantity, DateTime.UtcNow); | |
Apply(productReceived); | |
Add(productReceived); | |
} | |
private void Apply(ProductReceived evnt) | |
{ | |
_warehouseProductState.QuantityOnHand += evnt.Quantity; | |
} | |
public void AdjustInventory(int quantity, string reason) | |
{ | |
if (_warehouseProductState.QuantityOnHand + quantity < 0) | |
{ | |
throw new InvalidDomainException("Cannot adjust to a negative Quantity on Hand."); | |
} | |
var inventoryAdjusted = new InventoryAdjusted(Sku, quantity, reason, DateTime.UtcNow); | |
Apply(inventoryAdjusted); | |
Add(inventoryAdjusted); | |
} | |
private void Apply(InventoryAdjusted evnt) | |
{ | |
_warehouseProductState.QuantityOnHand += evnt.Quantity; | |
} | |
public WarehouseProductState GetState() | |
{ | |
return _warehouseProductState; | |
} | |
public int GetQuantityOnHand() | |
{ | |
return _warehouseProductState.QuantityOnHand; | |
} | |
} | |
public class InvalidDomainException : Exception | |
{ | |
public InvalidDomainException(string message) : base(message) | |
{ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment