Beispiel #1
0
        /// <summary>
        /// Handles all types of command asynchronously.
        /// </summary>
        /// <param name="command">The command.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">If command is null</exception>
        /// <exception cref="ArgumentOutOfRangeException">If command has unsupported type</exception>
        /// <exception cref="Exception">In case of more generic exception</exception>
        public async Task <BaseItemEvent> HandleAsync(BaseItemCommand command)
        {
            try
            {
                if (command == null)
                {
                    throw new ArgumentNullException(nameof(command));
                }

                BaseItemEvent evt;
                switch (command)
                {
                case CreateItemCommand create:
                    evt = new ItemCreatedEvent(create.Item.Id, create.Item.Name, command.UserName, Guid.NewGuid(), DateTime.UtcNow);
                    break;

                case DeleteItemCommand delete:
                    evt = new ItemDeletedEvent(delete.ItemId, delete.UserName, Guid.NewGuid(), DateTime.UtcNow);
                    break;

                case UpdateItemCommand update:
                    evt = new ItemUpdatedEvent(update.Item.Id, update.Item.Name, update.UserName, Guid.NewGuid(), DateTime.UtcNow);
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(command));
                }

                var applicationResult = await this.root.ApplyEventAsync(evt);

                switch (applicationResult)
                {
                case SuccessAggregateApplicationResult success:
                    await this.root.CommitAsync();

                    await this.publisher.PublishAsync(evt);

                    return(evt);

                case ItemNotFoundApplicationResult notFound:
                    throw new ItemNotFoundException();

                case ItemAlreadyExistsApplicationResult itemExists:
                    throw new ItemAlreadyExistsException(itemExists.Message);

                case FailedAggregateApplicationResult failed:
                    throw new Exception(failed.Error);

                default:
                    throw new ArgumentOutOfRangeException(nameof(applicationResult));
                }
            }
            catch (Exception e)
            {
                await this.root.RollbackAsync();

                this.logger.LogError(e, $"Failed to process command {command.GetType().Name}");
                throw;
            }
        }
 public void Handle(ItemDeletedEvent e)
 {
 }
Beispiel #3
0
 internal void Publish(ItemDeletedEvent itemDeletedEvent)
 {
     _bus.Publish(itemDeletedEvent);
 }
 public void Handle(ItemDeletedEvent e)
 {
     //Id = e.AggregateId;
 }
Beispiel #5
0
 public virtual void Delete(ItemDeletedEvent message)
 {
 }
 public void Handle(ItemDeletedEvent @event)
 {
     hub.Clients.All.globalMessageRecieved(@event);
 }