Пример #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);
        }
        public void Command_Is_Not_Stored_If_The_Handler_Is_Not_Successful()
        {
            Guid id = Guid.NewGuid();

            Assert.Throws <NotImplementedException>(() => _commandProcessor.Send(new MyCommandToFail()
            {
                Id = id
            }));

            _inbox.Exists <MyCommandToFail>(id, typeof(MyStoredCommandToFailHandler).FullName).Should().BeFalse();
        }
        public void When_Building_A_Pipeline_With_Global_inbox()
        {
            //act
            _chainOfResponsibility = _chainBuilder.Build(_requestContext);

            var firstHandler = _chainOfResponsibility.First();
            var myCommmand   = new MyCommand();

            firstHandler.Handle(myCommmand);

            //assert
            var exists = _inbox.Exists <MyCommand>(myCommmand.Id, CONTEXT_KEY, 500);

            exists.Should().BeTrue();
        }