Beispiel #1
0
        public void Should_Get_ConnectCommandHandler_When_Command_Is_Connect()
        {
            var command = new ConnectCommand();

            var handler = CommandHandlerFactory.GetHandler(command);

            Assert.IsAssignableFrom <ConnectCommandHandler>(handler);
        }
Beispiel #2
0
        public ICommandBus Send <T>(T command) where T : ICommand
        {
            var handler = _commandHandlerFactory.GetHandler <T>();

            handler.Execute(command);
            return(this);
        }
        public void InvokeCommandsQueue()
        {
            if (!commandsList.Any())
            {
                throw new EmptyCommandsQueueException("No commands in queue");
            }

            try
            {
                foreach (var command in commandsList)
                {
                    var handler       = commandHandlerFactory.GetHandler(command);
                    var sendingMethod = handler.GetType().GetRuntimeMethods().FirstOrDefault(m => m.Name == "Handle");

                    if (sendingMethod != null)
                    {
                        sendingMethod.Invoke(handler, new object[] { command });
                    }
                    else
                    {
                        throw new Exception($"Handle method not found on {handler.ToString()} object");
                    }
                }

                unitOfWork.SaveChanges();
            }
            catch (TargetInvocationException ex)
            {
                throw ex.InnerException;
            }
            catch (Exception)
            {
                throw;
            }
        }
Beispiel #4
0
        /// <summary>
        /// Simple direct call handler, can be replaced by event bus or any other messaging service
        /// </summary>
        /// <typeparam name="TCommand"></typeparam>
        /// <param name="command"></param>
        /// <returns></returns>
        public ICommandResult Send <TCommand>(TCommand command) where TCommand : ICommand
        {
            var handler = _commandHandlerFactory.GetHandler <TCommand>();

            if (handler != null)
            {
                return(handler.Execute(command));
            }
            throw new CommandHandlerNotFoundException(typeof(TCommand));
        }
Beispiel #5
0
        public void Execute <T>(T command) where T : Command
        {
            var handler = _commandHandlerFactory.GetHandler <T>();

            if (handler == null)
            {
                throw new Exception("no handler registered");
            }

            handler.Execute(command);
        }
Beispiel #6
0
        public void Send <T>(T command) where T : ICommand
        {
            var handler = handlerFactory.GetHandler <T>();

            if (handler == null)
            {
                throw new Exception("未找到对应的处理器");
            }

            handler.Execute(command);
        }
Beispiel #7
0
        public void Execute <T>(T command) where T : ICommand
        {
            var handler = _commandHandlerFactory.GetHandler(command);

            if (handler == null)
            {
                throw new InvalidOperationException($"Command `{command.GetType().FullName}` handler not found.");
            }

            handler.Handle(command);
        }
Beispiel #8
0
        /// <summary>
        /// Sends the async.
        /// </summary>
        /// <returns>The async.</returns>
        /// <param name="command">Command.</param>
        /// <typeparam name="TCommand">The 1st type parameter.</typeparam>
        public async Task SendAsync <TCommand>(TCommand command) where TCommand : Command
        {
            var handler = commandHandlerFactory.GetHandler <TCommand>();

            if (null == handler)
            {
                throw new UnregisteredHandlerException($"The handler of {typeof(TCommand).Name} is not registered");
            }

            await handler.HandleAsync(command);
        }
        public void Run()
        {
            while (true)
            {
                Console.Write(">");
                var inputcommand = Console.ReadLine();

                var handler = _handlerFactory.GetHandler(inputcommand);

                handler?.ProcessCommand(inputcommand);
            }
        }
Beispiel #10
0
        public void Send <T>(T command) where T : Command
        {
            var handler = _commandHandlerFactory.GetHandler <T>();

            if (handler != null)
            {
                handler.Execute(command);
            }
            else
            {
                throw new Exception();
            }
        }
        public void Send <T>(T command) where T : Command
        {
            var handler = _commandHandlerFactory.GetHandler <T>();

            if (handler != null)
            {
                handler.Execute(command);
            }
            else
            {
                throw new UnregisteredDomainCommandException("no handler registered");
            }
        }
Beispiel #12
0
        public async Task Send <T>(T command) where T : Command
        {
            var handler = _commandHandlerFactory.GetHandler <T>();

            if (handler != null)
            {
                await handler.Execute(command).ConfigureAwait(false);
            }
            else
            {
                throw new UnregisteredDomainEventException("no handler registered");
            }
        }
Beispiel #13
0
        public CommandResult Send <T>(T command) where T : Command
        {
            var handler = _commandHandlerFactory.GetHandler <T>();

            if (handler != null)
            {
                return(handler.Execute(command));
            }
            else
            {
                throw new UnregisteredDomainCommandException(string.Concat("No command handler registered for \"", command.ToString(), "\""));
            }
        }
Beispiel #14
0
        public async Task ProcessMessageAsync(Command command)
        {
            try
            {
                var handler = CommandHandlerFactory.GetHandler(command);

                ThrowsExceptionWhenHandlerIsNull(handler);

                await handler.ProcessAsync(command);
            }
            catch (Exception exception)
            {
                await ChatService.SendNoticeMessageAsync(command.ConnectionUid, exception.Message);
            }
        }
        private ICommandHandler GetHandler(ICommand command)
        {
            Contract.Requires(command != null);
            Contract.Ensures(Contract.Result <ICommandHandler>() != null);

            try
            {
                // get handler from factory
                return(_handlerFactory.GetHandler(command.GetType()));
            }
            catch (Exception e)
            {
                // rethrow exception has a command handling exception
                throw new CommandHandlingException(command, e, "Could not get command handler.");
            }
        }
Beispiel #16
0
        public async Task Dispatch <T>(T command)
            where T : class, ICommand
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            var handler = _factory.GetHandler <T>();

            if (handler == null)
            {
                throw new CommandHandlerNotFoundException($"command handler {nameof(T)} was not found");
            }

            await handler.Invoke(command);
        }
        public void Send <TCommand>(TCommand command)
        {
            ICommandHandler <TCommand> commandHandler = _commandHandlerFactory.GetHandler <TCommand>();

            commandHandler.Process(command);
        }
Beispiel #18
0
 public void CanGetHandlerForRegisteredCommand()
 {
     Assert.IsNotNull(_factory.GetHandler(typeof(Command1)));
 }
Beispiel #19
0
 public async Task SendAsync <T>(T command) where T : Command
 {
     ICommandHandler <T> handler = _factory.GetHandler <T>();
     await handler.ExecuteAsync(command);
 }