Пример #1
0
        /// <summary>
        /// Logs the command we received to the inbox.
        /// If the Once Only flag is set, it will reject commands that it has already seen from the pipeline
        /// </summary>
        /// <param name="command">The command that we want to store.</param>
        /// <returns>The parameter to allow request handlers to be chained together in a pipeline</returns>
        public override T Handle(T command)
        {
            if (_onceOnly)
            {
                s_logger.LogDebug("Checking if command {Id} has already been seen", command.Id);

                var exists = _inbox.Exists <T>(command.Id, _contextKey);

                if (exists && _onceOnlyAction is OnceOnlyAction.Throw)
                {
                    s_logger.LogDebug("Command {Id} has already been seen", command.Id);
                    throw new OnceOnlyException($"A command with id {command.Id} has already been handled");
                }

                if (exists && _onceOnlyAction is OnceOnlyAction.Warn)
                {
                    s_logger.LogWarning("Command {Id} has already been seen", command.Id);
                    return(command);
                }
            }

            T handledCommand = base.Handle(command);

            s_logger.LogDebug("Writing command {Id} to the Inbox", command.Id);

            _inbox.Add(command, _contextKey);

            return(handledCommand);
        }