/// <summary>
        /// 处理幂等命令
        /// </summary>
        /// <param name="message">幂等命令</param>
        /// <returns></returns>
        public async Task <TResult> Handle(IdentifiedCommand <TCommand, TResult> identifiedCommand)
        {
            var alreadyExists = await _requestManager.ExistAsync(identifiedCommand.Id);

            if (alreadyExists)
            {
                return(this.CreateResultForDuplicateRequest());
            }
            else
            {
                await _requestManager.CreateRequestForCommandAsync <TCommand>(identifiedCommand.Id);

                var result = await this._mediator.Send(identifiedCommand.Command);

                return(result);
            }
        }
Exemple #2
0
        /// <summary>
        /// This method handles the command. It just ensures that no other request exists with the same ID, and if this is the case
        /// just enqueues the original inner command.
        /// </summary>
        /// <param name="message">IdentifiedCommand which contains both original command & request ID</param>
        /// <returns>Return value of inner command or default value if request same ID was found</returns>
        public async Task <R> Handle(IdentifiedCommand <T, R> message, CancellationToken cancellationToken)
        {
            var alreadyExists = await _requestManager.ExistAsync(message.Id);

            if (alreadyExists)
            {
                return(CreateResultForDuplicateRequest());
            }

            await _requestManager.CreateRequestForCommandAsync <T>(message.Id);

            try
            {
                // Send the embeded business command to mediator so it runs its related CommandHandler
                var result = await _mediator.Send(message.Command);

                return(result);
            }
            catch
            {
                return(default(R));
            }
        }
        /// <summary>
        /// This method handles the command. It just ensures that no other request exists with the same ID, and if this is the case
        /// just enqueues the original inner command.
        /// </summary>
        /// <param name="message">IdentifiedCommand which contains both original command & request ID</param>
        /// <returns>Return value of inner command or default value if request same ID was found</returns>
        public async Task <R> Handle(IdentifiedCommand <T, R> message, CancellationToken cancellationToken)
        {
            var alreadyExists = await _requestManager.ExistAsync(message.Id);

            if (alreadyExists)
            {
                return(CreateResultForDuplicateRequest());
            }
            else
            {
                await _requestManager.CreateRequestForCommandAsync <T>(message.Id);

                try
                {
                    var command     = message.Command;
                    var commandName = command.GetGenericTypeName();
                    var idProperty  = string.Empty;
                    var commandId   = string.Empty;

                    switch (command)
                    {
                    case CreateOrderCommand createOrderCommand:
                        idProperty = nameof(createOrderCommand.UserId);
                        commandId  = createOrderCommand.UserId;
                        break;

                    case CancelOrderCommand cancelOrderCommand:
                        idProperty = nameof(cancelOrderCommand.OrderNumber);
                        commandId  = $"{cancelOrderCommand.OrderNumber}";
                        break;

                    case ShipOrderCommand shipOrderCommand:
                        idProperty = nameof(shipOrderCommand.OrderNumber);
                        commandId  = $"{shipOrderCommand.OrderNumber}";
                        break;

                    default:
                        idProperty = "Id?";
                        commandId  = "n/a";
                        break;
                    }

                    _logger.LogInformation(
                        "----- Sending command: {CommandName} - {IdProperty}: {CommandId} ({@Command})",
                        commandName,
                        idProperty,
                        commandId,
                        command);

                    // Send the embeded business command to mediator so it runs its related CommandHandler
                    var result = await _mediator.Send(command, cancellationToken);

                    _logger.LogInformation(
                        "----- Command result: {@Result} - {CommandName} - {IdProperty}: {CommandId} ({@Command})",
                        result,
                        commandName,
                        idProperty,
                        commandId,
                        command);

                    return(result);
                }
                catch
                {
                    return(default(R));
                }
            }
        }