Exemplo n.º 1
0
        public async Task Handle(CreateInventoryItem command)
        {
            InventoryItemEvents iie = new InventoryItemEvents(_inventoryEventRepository, command.AggregateId);

            var eventData = ((CreateInventoryItemData)command.EventData).InventoryItemData;

            if (await iie.ModelEventsCountAsync(command.AggregateId) > 0)
            {
                //if (await _inventoryEventRepository.ModelEventsCountAsync(command.AggregateId) > 0)
                throw new DuplicateAggregateException(
                          string.Format("AggregateId {0} already created an inventory item.", command.AggregateId));
            }

            await Task.WhenAll(
                iie.AppendEventAsync((IModelEvent <Guid>)command),
                _inventoryWriteRepository.AppendAsync(
                    command.AggregateId,
                    new InventoryItemDto()
            {
                Id = command.AggregateId,
                LastEventTimestamp = command.Timestamp,
                Name     = eventData.Name,
                IsActive = eventData.IsActive,
                Count    = eventData.Count,
                Note     = eventData.Note
            }
                    ));
        }
Exemplo n.º 2
0
        public async Task <IEnumerable <AInventoryItemEvent> > InventoryEventsAsync(Guid id)
        {
            InventoryItemEvents iie = new InventoryItemEvents(_inventoryEventRepository, id);

            var result = iie.EventsAsync().Result;

            return(await Task.FromResult(result.Cast <AInventoryItemEvent>()));
        }
Exemplo n.º 3
0
        public async Task Handle(DeleteInventoryItem command)
        {
            InventoryItemEvents iie = new InventoryItemEvents(_inventoryEventRepository, command.AggregateId);

            // ToDo: Cache aggregate and attempt to update rather than regenerate
            InventoryItemAggregate iia = (InventoryItemAggregate)await iie.ModelAsync();

            await Task.WhenAll(
                iie.AppendEventAsync((IModelEvent <Guid>)command)
                , _inventoryWriteRepository.DeleteAsync(command.AggregateId)
                );
        }
Exemplo n.º 4
0
        private async Task _HandleUpdate(AInventoryItemEvent command)
        {
            InventoryItemEvents iie = new InventoryItemEvents(_inventoryEventRepository, command.AggregateId);

            // ToDo: Cache aggregate and attempt to update rather than regenerate
            InventoryItemAggregate iia = (InventoryItemAggregate)await iie.ModelAsync();

            command.ApplyEventData(iia);

            await Task.WhenAll(
                iie.AppendEventAsync((IModelEvent <Guid>)command),
                _inventoryWriteRepository.UpdateAsync(
                    command.AggregateId,
                    new InventoryItemDto(iia)
                    ));
        }
Exemplo n.º 5
0
        public async Task Confirm_Final_Aggregate_Model()
        {
            Guid id = Guid.NewGuid();

            InventoryItemDto item = new InventoryItemDto()
            {
                Id = id,
                LastEventTimestamp = DateTime.UtcNow,
                Name     = "",
                IsActive = false,
                Count    = 0,
                Note     = ""
            };

            // Create Inventory Item
            await InvokeInventoryEventRepository().AppendEventAsync(new CreateInventoryItem(item));

            // Activate
            await InvokeInventoryEventRepository().AppendEventAsync(new ActivateInventoryItem(id, new SetInventoryItemActivation()));

            // Deactivate
            await InvokeInventoryEventRepository().AppendEventAsync(new DeactivateInventoryItem(id, new SetInventoryItemActivation()));

            // Update Full Item
            await InvokeInventoryEventRepository().AppendEventAsync(new UpdateInventoryItem(new InventoryItemDto()
            {
                Id = id,
                LastEventTimestamp = DateTime.UtcNow,
                Name     = "UpdatedItem",
                IsActive = true,
                Count    = 2,
                Note     = "N/A"
            }));

            // Set Count
            await InvokeInventoryEventRepository().AppendEventAsync(new SetInventoryItemCount(
                                                                        id, new SetInventoryItemCountData()
            {
                Count = 10
            }));

            // Increase Count by 1
            await InvokeInventoryEventRepository().AppendEventAsync(new IncreaseInventoryItemCount(
                                                                        id, new AdjustInventoryItemCount()
            {
                Delta = 1
            }));

            // Decrease Count by 3
            await InvokeInventoryEventRepository().AppendEventAsync(new DecreaseInventoryItemCount(
                                                                        id, new AdjustInventoryItemCount()
            {
                Delta = 3
            }));

            // Set Name
            await InvokeInventoryEventRepository().AppendEventAsync(new SetInventoryItemName(
                                                                        id, new SetInventoryItemNameData()
            {
                Name = "Name"
            }));

            // Set Note
            await InvokeInventoryEventRepository().AppendEventAsync(new SetInventoryItemNote(
                                                                        id, new SetInventoryItemNoteData()
            {
                Note = "Note"
            }));

            // Confirm Final Model
            InventoryItemEvents    iie = new InventoryItemEvents(InvokeInventoryEventRepository(), id);
            InventoryItemAggregate iia = (InventoryItemAggregate)await iie.ModelAsync();

            Assert.IsTrue(iia.Name.Equals("Name", StringComparison.Ordinal));
            Assert.IsTrue(iia.IsActive);
            Assert.IsTrue(iia.Count == 8);
            Assert.IsTrue(iia.Note.Equals("Note", StringComparison.Ordinal));

            // Delete Item
            await InvokeInventoryEventRepository().AppendEventAsync(new DeleteInventoryItem(id));

            // Confirm Nb of Events
            int eventCount = await InvokeInventoryEventRepository().ModelEventsCountAsync(id);

            Assert.IsTrue(eventCount == 10);
        }