Skip to content

Instantly share code, notes, and snippets.

@dcomartin

dcomartin/WarehouseProduct.cs Secret

Created May 20, 2021
Embed
What would you like to do?
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