示例#1
0
        public void Dispatch <TCommand>(TCommand command)
            where TCommand : ICommand
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            ICommandHandler <TCommand> commandHandler;

            try
            {
                commandHandler = CommandHandlerFactory.Create <TCommand>();
            }
            catch (Exception ex)
            {
                throw new CannotCreateCommandHandlerException(ex);
            }

            if (commandHandler == null)
            {
                throw new CannotCreateCommandHandlerException();
            }

            this.OnBeforeExecuteHandler(commandHandler, command);

            commandHandler.Execute(command);
        }
        public ICommandChain ContinueWith <TContinuationCommand>(TContinuationCommand command) where TContinuationCommand : ICommand
        {
            var continuationCommandHandler = _commandHandlerFactory.Create <TContinuationCommand>();

            return(new ContinuationCommandChain(_commandHandlerFactory, _objectMapper, () =>
            {
                _commandDelegate();
                continuationCommandHandler.Execute(command);
            }));
        }
示例#3
0
        /// <summary>
        /// Executes a command handler based on the command type.
        /// </summary>
        /// <typeparam name="TCommand">A command type</typeparam>
        /// <param name="command">An instance of a command with data</param>
        public void Execute <TCommand>(TCommand command) where TCommand : ICommand
        {
            var commandHandler = _commandHandlerFactory.Create <TCommand>();

            try
            {
                commandHandler.CommandExecuted += CommandExecuted;
                commandHandler.Execute(command);
            }
            finally
            {
                _commandHandlerFactory.Release(commandHandler);
            }
        }
示例#4
0
        public void ProcessCommand <TCommand>(TCommand command)
            where TCommand : class, ICommand
        {
            var handler = factory.Create <TCommand>();

            handler.Handle(command);
        }
示例#5
0
        public CommandResult <TResult> Execute <TCommand, TResult>(TCommand command)
            where TCommand : ICommand <TResult>
        {
            var commandHandler = _commandHandlerFactory.Create <TCommand, TResult>();

            return(commandHandler.Handle(command));
        }
        private void HandleCommand <TCommand>(RingCommandContext context, ICommand command) where TCommand : ICommand
        {
            var handlerType = _commandRegister.FindHandlerType <TCommand>();

            if (handlerType == null)
            {
                var message = $"命令[{command.GetType().Name}]的命令执行器没有找到";
                _logger.LogInformation(message);
                throw new DomainException(DomainCode.CommandHandlerNotExists, message);
            }
            ICommandHandler <TCommand> handler;

            try
            {
                handler = _commandHandlerFactory.Create <TCommand>(handlerType);
            }
            catch (Exception ex)
            {
                var message = $"命令[{command.GetType().Name}]的命令执行器无法创建,原因:{ex.Message}";
                _logger.LogInformation(message);
                throw new DomainException(DomainCode.CommandHandlerCannotCreate, message);
            }


            handler.Handle(context, (TCommand)command);
        }
示例#7
0
        private void ExecuteCommand <TCommand>(TCommand command, IExecutionContext executionContext) where TCommand : ICommand
        {
            if (command == null)
            {
                return;
            }

            var cx      = CreateExecutionContext(executionContext);
            var handler = _commandHandlerFactory.Create <TCommand>();

            if (handler == null)
            {
                throw new MissingHandlerMappingException(typeof(TCommand));
            }

            try
            {
                _modelValidationService.Validate(command);
                _executePermissionValidationService.Validate(command, handler, cx);
                handler.Execute(command, cx);
            }
            catch (Exception ex)
            {
                _commandLogService.LogFailed(command, cx, ex);
                throw;
            }

            _commandLogService.Log(command, cx);
        }
        private async Task <TResult> ExecuteCommandWithHandlers <TResult>(ICommand <TResult> command, ICommandDispatchContext dispatchContext, CancellationToken cancellationToken)
        {
            IReadOnlyCollection <IPrioritisedCommandHandler> handlers = _commandRegistry.GetPrioritisedCommandHandlers(command);

            if (handlers == null || handlers.Count == 0)
            {
                throw new MissingCommandHandlerRegistrationException(command.GetType(),
                                                                     "No command actors registered for execution of command");
            }
            TResult result = default(TResult);

            int handlerIndex = 0;

            foreach (IPrioritisedCommandHandler handlerTemplate in handlers)
            {
                object baseHandler = null;
                try
                {
                    baseHandler = _commandHandlerFactory.Create(handlerTemplate.CommandHandlerType);

                    if (baseHandler is ICommandHandler handler)
                    {
                        result = await _commandHandlerExecuter.ExecuteAsync(handler, command, result, cancellationToken);
                    }
                    else
                    {
                        if (baseHandler is IPipelineAwareCommandHandler chainHandler)
                        {
                            PipelineAwareCommandHandlerResult <TResult> chainResult =
                                await _pipelineAwareCommandHandlerExecuter.ExecuteAsync(chainHandler, command, result, cancellationToken);

                            result = chainResult.Result;
                            if (chainResult.ShouldStop)
                            {
                                break;
                            }
                        }
                        else
                        {
                            throw new UnableToExecuteHandlerException("Unexpected result type");
                        }
                    }
                }
                catch (Exception ex)
                {
                    bool shouldContinue =
                        await _commandExecutionExceptionHandler.HandleException(ex, baseHandler, handlerIndex, command,
                                                                                dispatchContext);

                    if (!shouldContinue)
                    {
                        break;
                    }
                }
                handlerIndex++;
            }

            return(result);
        }
示例#9
0
        private ICommandHandler GetHandler(ICommand command)
        {
            var type        = command.GetType();
            var handlerType = _registry.GetHandler(type);
            var handler     = _factory.Create(handlerType);

            return(handler);
        }
示例#10
0
        public void Execute <TCommand>(TCommand command) where TCommand : ICommand
        {
            ICommandHandler <TCommand> handler = _HandlerFactory.Create <TCommand>();

            try {
                handler.Execute(command);
            } finally {
                _HandlerFactory.Release(handler);
            }
        }
示例#11
0
        public Task <TCommandResult> Dispatch <TCommand, TCommandResult>(TCommand command) where TCommand : class where TCommandResult : class
        {
            var commandHandler = commandHandlerFactory.Create <TCommand, TCommandResult>(command);

            try
            {
                return(commandHandler.Handle(command));
            }
            finally
            {
                commandHandlerFactory.Destroy(commandHandler);
            }
        }
示例#12
0
        public void Process <T>(T command)
        {
            var handler = (IHandleCommands <T>)_commandHandlerFactory.Create <T>();

            var attributes = GetAttributes <T>(handler);

            RunBeforeAttributes(command, attributes);

            handler.Handle(command);
            _commandHandlerFactory.Dispose(handler);

            RunPostAttributesAndDispose(command, attributes);
        }
示例#13
0
        /// <summary>
        /// Helper method to execute any ICommandHandler instance in a generic
        /// way.
        /// </summary>
        /// <param name="handlerTypeTemplate">
        /// Generic Handler Type template that we fill in (with the handler's
        /// TCommand and TResult) and use to resolve an instance of our desired
        /// <see cref="ICommandHandler" /> using the
        /// <see cref="ICommandHandlerFactory" />.
        /// </param>
        /// <param name="executeMethodTemplate">
        /// The pre-reflected template method used to call the actual generic
        /// Execute method of the <see cref="ICommandHandler" /> instance of
        /// unknown exact type.
        /// </param>
        /// <param name="commandInstance">
        /// The <see cref="ICommand" /> to be
        /// handled by the <see cref="ICommandHandler" />.</param>
        /// <param name="genericTemplateArgs">
        /// An array of Types to be used as generic type parameters when
        /// when filling in the <see paramRef="handlerTypeTemplate" /> and
        /// <see paramRef="executeMethodTemplate" />.
        /// </param>
        /// <returns>
        /// The results of executing the <see cref="ICommandHandler" />.
        /// </returns>
        private object ExecuteCommand(
            Type handlerTypeTemplate,
            MethodInfo executeMethodTemplate,
            object commandInstance,
            params Type[] genericTemplateArgs
            )
        {
            // Fill in handlerTypeTemplate with genericTemplateArgs to get the
            // generic handlerType we want to resolve
            var handlerType =
                handlerTypeTemplate.MakeGenericType(genericTemplateArgs);

            // Resolve the handlerInstance
            var handlerInstance = handlerFactory.Create(handlerType);

            // Fill in the handler's executeMethodTemplate with the same
            // genericTemplateArgs to get a method instance we can execute
            // against the resolved handlerInstance
            var executeMethod =
                executeMethodTemplate.MakeGenericMethod(genericTemplateArgs);

            try
            {
                return
                    (executeMethod.Invoke(
                         null,
                         new[] { handlerInstance, commandInstance }
                         ));
            }
            catch (TargetInvocationException ex)
            {
                throw new CommandExecutionException(commandInstance, ex.InnerException);
            }
            finally
            {
                handlerFactory.Release(handlerInstance);
            }
        }
示例#14
0
        public CommandResult <TReturn> Execute <TCommand, TReturn>(TCommand command)
        {
            ICommandHandler <TCommand, TReturn> handler = _commandHandlerFactory.Create <TCommand, TReturn>();

            try
            {
                var returnValue = handler.Handle(command);
                return(CommandResult <TReturn> .Executed(returnValue));
            }
            finally
            {
                _commandHandlerFactory.Destroy(handler);
            }
        }
示例#15
0
        public IGatewayCommandsConfigurator Handle <TCommand>() where TCommand : ICommand
        {
            for (int i = 0; i < _commandHandlerThreadCount; i++)
            {
                _bus.SubscribeAsync(
                    typeof(E <TCommand>),
                    Assembly.GetEntryAssembly().GetName().Name,
                    incomingCommand => Task.Factory.StartNew(() =>
                {
                    Envelope <TCommand> command = (E <TCommand>)incomingCommand;
                    var commandHandler          = _commandHandlerFactory.Create <Envelope <TCommand> >();
                    commandHandler.Execute(command);
                }));
            }

            return(this);
        }
示例#16
0
        public int ExecuteCommand(params string[] args)
        {
            try
            {
                var command        = _commandFactory.Create(args);
                var commandHandler = _commandHandlerFactory.Create(command);
                commandHandler.Handle(command);

                if (ShowSuccess(command))
                {
                    _output.WriteSuccess("{0} success.", command.GetType().Name);
                }
            }
            catch (Exception exception)
            {
                _output.WriteError("error: {0}", exception.Message);
                return(-1);
            }
            return(0);
        }
示例#17
0
        public void Execute <TCommand>(TCommand command)
        {
            var handler = _factory.Create <TCommand>();

            handler.Handle(command);
        }
示例#18
0
 public async Task Handle <TCommand>(TCommand command) where TCommand : ICommand
 {
     var handler = _queryHandlerFactory.Create(command);
     await handler.Handle(command);
 }
示例#19
0
        //public void Handle<TCommand>(TCommand command) where TCommand : ICommand
        //{
        //    var handler = _factory.Create<TCommand>();

        //    handler.Handle(command);
        //}

        void ICommandHandlerDispatcher.Handle <TCommand>(TCommand command)
        {
            var handler = _factory.Create <TCommand>();

            handler.Handle(command);
        }
示例#20
0
        public void Send <T>(T command)
        {
            var commandHandler = _handlerFactory.Create <T>();

            commandHandler.Handle(command);
        }
 public async Task ExecuteAsync <TCommandArgs>(TCommandArgs args)
 {
     var handler = _commandHandlerFactory.Create <TCommandArgs>();
     await handler.ExecuteAsync(args).ConfigureAwait(false);
 }
示例#22
0
        public void Process <T>(T command)
        {
            ICommandHandler <T> handler = Factory.Create <T>();

            handler.Handle(command);
        }