Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created February 17, 2021 22:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcomartin/dfd959ef91626b3d6e07b762bc925edc to your computer and use it in GitHub Desktop.
Save dcomartin/dfd959ef91626b3d6e07b762bc925edc to your computer and use it in GitHub Desktop.
public class WarehouseProductRepository
{
private readonly Dictionary<string, List<IEvent>> _inMemoryStreams = new();
public WarehouseProduct Get(string sku)
{
var warehouseProduct = new WarehouseProduct(sku);
if (_inMemoryStreams.ContainsKey(sku))
{
foreach (var evnt in _inMemoryStreams[sku])
{
warehouseProduct.ApplyEvent(evnt);
}
}
return warehouseProduct;
}
public void Save(WarehouseProduct warehouseProduct)
{
if (_inMemoryStreams.ContainsKey(warehouseProduct.Sku) == false)
{
_inMemoryStreams.Add(warehouseProduct.Sku, new List<IEvent>());
}
var newEvents = warehouseProduct.GetUncommittedEvents();
_inMemoryStreams[warehouseProduct.Sku].AddRange(newEvents);
warehouseProduct.EventsCommitted();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment