Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created Feb 24, 2021
Embed
What would you like to do?
public class ProjectionBuilder
{
private readonly ProductDbContext _dbContext;
public ProjectionBuilder(ProductDbContext dbContext)
{
_dbContext = dbContext;
}
public void ReceiveEvent(IEvent evnt)
{
switch (evnt)
{
case ProductShipped shipProduct:
Apply(shipProduct);
break;
case ProductReceived receiveProduct:
Apply(receiveProduct);
break;
}
}
public Product GetProduct(string sku)
{
var product = _dbContext.Products.SingleOrDefault(x => x.Sku == sku);
if (product == null)
{
product = new Product
{
Sku = sku
};
_dbContext.Products.Add(product);
}
return product;
}
private void Apply(ProductShipped shipProduct)
{
var product = GetProduct(shipProduct.Sku);
product.Shipped += shipProduct.Quantity;
_dbContext.SaveChanges();
}
private void Apply(ProductReceived productReceived)
{
var state = GetProduct(productReceived.Sku);
state.Received += productReceived.Quantity;
_dbContext.SaveChanges();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment