private Dictionary <Guid, List <IEvent> > AddOrderLines(OrderAggregate orderAggregate, IEnumerable <OrderLineDto> orderLines) { IEnumerable <WarehouseView> warehouses = repository.Query <WarehouseView, WarehouseView>((w) => new WarehouseView { Id = w.Id, Items = w.Items }); var events = new Dictionary <Guid, List <IEvent> >(); foreach (OrderLineDto orderLine in orderLines) { WarehouseView warehouse = warehouses.FirstOrDefault(w => w.Items.Any(item => item.ProductId == orderLine.ProductNumber && item.Quantity >= orderLine.Quantity)); var orderItem = new OrderLine { ProductId = orderLine.ProductNumber, Quantity = orderLine.Quantity }; if (warehouse != null) { WarehouseAggregate warehouseAggregate = repository.Load <WarehouseAggregate>(warehouse.Id); warehouseAggregate.SubstractItemQuantity(orderLine.ProductNumber, orderLine.Quantity); AddAggEvents(events, warehouseAggregate.Id, warehouseAggregate.PendingEvents.ToList()); orderItem.WarehouseId = warehouseAggregate.Id; } else { orderItem.Status = OrderLineStatus.OutOfStock.ToString(); } orderAggregate.AddLine(orderItem); AddAggEvents(events, orderAggregate.Id, orderAggregate.PendingEvents.ToList()); } return(events); }