Пример #1
0
        /// <summary>
        /// 执行命令
        /// </summary>
        /// <typeparam name="TCommand">命令类型</typeparam>
        /// <typeparam name="TPrimaryKey">主键类型</typeparam>
        /// <param name="command">命令对象</param>
        /// <returns>命令结果对象</returns>
        public CommandResult Execute <TCommand, TPrimaryKey>(TCommand command) where TCommand : ICommand <TPrimaryKey>
        {
            ICommandContext context = ObjectContainer.Resolve <ICommandContext>();

            CommandResult commandResult;

            try
            {
                ICommandHandler <TCommand, TPrimaryKey> commandHandler = ObjectContainer.Resolve <ICommandHandler <TCommand, TPrimaryKey> >();

                TimeSpan ts = ActionHelper.StopwatchAction(() =>
                {
                    commandHandler.Handler(context, command);

                    context.Commit();
                });

                commandResult = new CommandResult(command.AggregateRoot, CommandStatus.Success, command.Id.ToString(), ts.Milliseconds);
            }
            catch (Exception ex)
            {
                ObjectContainer.Resolve <ILoggerFactory>().CreateLogger(typeof(CommandService)).Error("CommandService执行出错", ex);

                context.Rollback();

                commandResult = new CommandResult(command.AggregateRoot, CommandStatus.Failed, result: ex.Message);
            }
            finally
            {
                context.Dispose();
            }

            this.SaveCommand(commandResult);

            return(commandResult);
        }
Пример #2
0
        CommandResult Handle(ICommandContext commandContext, CommandRequest command)
        {
            var commandResult = new CommandResult();

            try
            {
                using (_localizer.BeginScope())
                {
                    _logger.Information($"Handle command of type {command.Type}");

                    commandResult = CommandResult.ForCommand(command);

                    _logger.Trace("Authorize");
                    var authorizationResult = _commandSecurityManager.Authorize(command);
                    if (!authorizationResult.IsAuthorized)
                    {
                        _logger.Trace("Command not authorized");
                        commandResult.SecurityMessages = authorizationResult.BuildFailedAuthorizationMessages();
                        commandContext.Rollback();
                        return(commandResult);
                    }

                    _logger.Trace("Validate");
                    var validationResult = _commandValidationService.Validate(command);
                    commandResult.ValidationResults         = validationResult.ValidationResults;
                    commandResult.CommandValidationMessages = validationResult.CommandErrorMessages;

                    if (commandResult.Success)
                    {
                        _logger.Trace("Command is considered valid");
                        try
                        {
                            _logger.Trace("Handle the command");
                            _commandHandlerManager.Handle(command);

                            _logger.Trace("Collect any broken rules");
                            commandResult.BrokenRules = commandContext.GetObjectsBeingTracked()
                                                        .SelectMany(_ => _.BrokenRules)
                                                        .Select(_ => new BrokenRuleResult(
                                                                    _.Rule.Name,
                                                                    $"EventSource: {_.Context.Target.GetType().Name} - with id {((IEventSource)_.Context.Target).EventSourceId.Value}",
                                                                    _.Instance?.ToString() ?? "[Not Set]",
                                                                    _.Causes));

                            _logger.Trace("Commit transaction");
                            commandContext.Commit();
                        }
                        catch (TargetInvocationException ex)
                        {
                            _logger.Error(ex, "Error handling command");
                            commandResult.Exception = ex.InnerException;
                            commandContext.Rollback();
                        }
                        catch (Exception ex)
                        {
                            _logger.Error(ex, "Error handling command");
                            commandResult.Exception = ex;
                            commandContext.Rollback();
                        }
                    }
                    else
                    {
                        _logger.Information("Command was not successful, rolling back");
                        commandContext.Rollback();
                    }
                }
            }
            catch (TargetInvocationException ex)
            {
                _logger.Error(ex, "Error handling command");
                commandResult.Exception = ex.InnerException;
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "Error handling command");
                commandResult.Exception = ex;
            }

            return(commandResult);
        }
Пример #3
0
 public void Dispose()
 {
     _commandContext.Commit();
 }