Skip to content

Instantly share code, notes, and snippets.

@dcomartin

dcomartin/Repository.cs Secret

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