Exemplo n.º 1
0
        private HandlerFindResult GetCommandHandler <T>(ProcessingCommand processingCommand, out T handlerProxy) where T : class, IObjectProxy
        {
            handlerProxy = null;

            var command         = processingCommand.Message;
            var handlerDataList = _commandHandlerProvider.GetHandlers(command.GetType());

            if (handlerDataList == null || handlerDataList.Count() == 0)
            {
                return(HandlerFindResult.NotFound);
            }
            else if (handlerDataList.Count() > 1)
            {
                return(HandlerFindResult.TooManyHandlerData);
            }

            var handlerData = handlerDataList.Single();

            if (handlerData.ListHandlers == null || handlerData.ListHandlers.Count() == 0)
            {
                return(HandlerFindResult.NotFound);
            }
            else if (handlerData.ListHandlers.Count() > 1)
            {
                return(HandlerFindResult.TooManyHandler);
            }

            handlerProxy = handlerData.ListHandlers.Single() as T;

            return(HandlerFindResult.Found);
        }
Exemplo n.º 2
0
        private static void CreateCommandHandlers(ICommandDispatcher commandDispatcher,
                                                  ICommandHandlerProvider commandHandlerProvider)
        {
            commandHandlerTypes = commandHandlerProvider.GetHandlers();

            if (commandHandlerTypes != null)
            {
                MethodInfo registerMethod = commandDispatcher.GetType().GetMethod("Register", BindingFlags.Instance | BindingFlags.Public);

                foreach (KeyValuePair <Type, Type> commandHandlerPair in commandHandlerTypes)
                {
                    if (!AppRuntime.Instance.CurrentApplication.ObjectContainer.Registered(commandHandlerPair.Value))
                    {
                        AppRuntime.Instance.CurrentApplication.ObjectContainer.RegisterType(commandHandlerPair.Value);
                    }

                    MethodInfo genericRegisterMethod = registerMethod.MakeGenericMethod(commandHandlerPair.Key);

                    genericRegisterMethod.Invoke(commandDispatcher,
                                                 new object[]
                    {
                        AppRuntime.Instance.CurrentApplication.ObjectContainer.Resolve(commandHandlerPair.Value)
                    });
                }
            }
        }
        public void Handle(ProcessingCommand processingCommand)
        {
            var command = processingCommand.Message;

            if (string.IsNullOrEmpty(command.AggregateRootId))
            {
                var errorMessage = string.Format("The aggregateRootId of command cannot be null or empty. commandType:{0}, commandId:{1}", command.GetType().Name, command.Id);
                _logger.Error(errorMessage);
                CompleteCommand(processingCommand, CommandStatus.Failed, typeof(string).FullName, errorMessage);
                return;
            }

            ICommandHandlerProxy commandHandler;
            var findResult = GetCommandHandler(processingCommand, commandType => _commandHandlerProvider.GetHandlers(commandType), out commandHandler);

            if (findResult == HandlerFindResult.Found)
            {
                HandleCommand(processingCommand, commandHandler);
            }
            else if (findResult == HandlerFindResult.TooManyHandlerData)
            {
                _logger.ErrorFormat("Found more than one command handler data, commandType:{0}, commandId:{1}", command.GetType().FullName, command.Id);
                CompleteCommand(processingCommand, CommandStatus.Failed, typeof(string).FullName, "More than one command handler data found.");
            }
            else if (findResult == HandlerFindResult.TooManyHandler)
            {
                _logger.ErrorFormat("Found more than one command handler, commandType:{0}, commandId:{1}", command.GetType().FullName, command.Id);
                CompleteCommand(processingCommand, CommandStatus.Failed, typeof(string).FullName, "More than one command handler found.");
            }
            else if (findResult == HandlerFindResult.NotFound)
            {
                ICommandAsyncHandlerProxy commandAsyncHandler;
                findResult = GetCommandHandler(processingCommand, commandType => _commandAsyncHandlerProvider.GetHandlers(commandType), out commandAsyncHandler);
                if (findResult == HandlerFindResult.Found)
                {
                    HandleCommand(processingCommand, commandAsyncHandler);
                }
                else if (findResult == HandlerFindResult.TooManyHandlerData)
                {
                    _logger.ErrorFormat("Found more than one command async handler data, commandType:{0}, commandId:{1}", command.GetType().FullName, command.Id);
                    CompleteCommand(processingCommand, CommandStatus.Failed, typeof(string).FullName, "More than one command async handler data found.");
                }
                else if (findResult == HandlerFindResult.TooManyHandler)
                {
                    _logger.ErrorFormat("Found more than one command async handler, commandType:{0}, commandId:{1}", command.GetType().FullName, command.Id);
                    CompleteCommand(processingCommand, CommandStatus.Failed, typeof(string).FullName, "More than one command async handler found.");
                }
                else if (findResult == HandlerFindResult.NotFound)
                {
                    var errorMessage = string.Format("No command handler found of command. commandType:{0}, commandId:{1}", command.GetType().Name, command.Id);
                    _logger.Error(errorMessage);
                    CompleteCommand(processingCommand, CommandStatus.Failed, typeof(string).FullName, errorMessage);
                }
            }
        }
        private ICommandHandlerProxy GetCommandHandler(ProcessingCommand processingCommand)
        {
            var command         = processingCommand.Message;
            var commandHandlers = _commandHandlerProvider.GetHandlers(command.GetType());

            if (commandHandlers.Count() > 1)
            {
                _logger.ErrorFormat("Found more than one command handlers, commandType:{0}, commandId:{1}.", command.GetType().FullName, command.Id);
                NotifyCommandExecuted(processingCommand, CommandStatus.Failed, null, "More than one command handlers found.");
                return(null);
            }

            return(commandHandlers.SingleOrDefault());
        }