Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created Oct 14, 2021
Embed
What would you like to do?
public class QuickOrderHandler2 : IRequestHandler<QuickOrderCommand>
{
private readonly IOrderRepository _orderRepository;
private readonly ISalesProductRepository _salesProductRepository;
public QuickOrderHandler2(IOrderRepository orderRepository, ISalesProductRepository salesProductRepository)
{
_orderRepository = orderRepository;
_salesProductRepository = salesProductRepository;
}
public async Task Handle(QuickOrderCommand request)
{
using var trx = _orderRepository.BeginTransaction(IsolationLevel.Serializable);
var salesProduct = await _salesProductRepository.SalesProduct(request.Sku, trx);
var quickOrder = salesProduct.CreateQuickOrder();
_orderRepository.Save(quickOrder, trx);
await _salesProductRepository.Save(salesProduct, request.Version);
trx.Commit();
}
}
public class SalesProduct
{
public Sku Sku { get; protected set; }
public decimal Price { get; protected set; }
public int AvailableToPromise { get; protected set; }
public SalesProduct(Sku sku, decimal price, int availableToPromise)
{
Sku = sku;
Price = price;
AvailableToPromise = availableToPromise;
}
public QuickOrder CreateQuickOrder()
{
AvailableToPromise = AvailableToPromise - 1;
return QuickOrder.Factory(this);
}
}
public class QuickOrder
{
public Sku Sku { get; }
public decimal Price { get; }
public QuickOrder(Sku sku, decimal price)
{
Sku = sku;
Price = price;
}
public QuickOrder(SalesProduct product)
{
Sku = product.Sku;
Price = product.Price;
}
public static QuickOrder Factory(SalesProduct salesProduct)
{
if (salesProduct.AvailableToPromise <= 0)
{
throw new InvalidOperationException("Product has no quantity on hand.");
}
return new QuickOrder(salesProduct);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment