public bool AddItem(string roomName, InventoryItem item, out InventoryItem droppedItem, ITextProcessor textProcessor) { Guard.NullParameter(roomName, () => roomName); Guard.NullParameter(item, () => item); using (var session = _documentStore.OpenSession()) { var inventory = session.Query<Inventory>().Customize(x => x.WaitForNonStaleResults()).SingleOrDefault(x => x.Room == roomName); if (inventory == null) { inventory = new Inventory { Room = roomName, Items = new List<InventoryItem>() }; session.Store(inventory); } droppedItem = null; if (inventory.Items.Any(x => textProcessor.SimplifiedEquals(x.Value, item.Value))) { session.SaveChanges(); return true; } if (inventory.Items.Count >= _inventoryCapacity) { var itemIndex = _randomNumberProvider.Next(_inventoryCapacity); droppedItem = inventory.Items[itemIndex]; inventory.Items.RemoveAt(itemIndex); } inventory.Items.Add(item); session.SaveChanges(); return false; } }