예제 #1
0
        CommandResult Handle(ITransaction transaction, ICommand command)
        {
            var commandResult = new CommandResult();

            try
            {
                using (_localizer.BeginScope())
                {
                    commandResult = CommandResult.ForCommand(command);

                    var authorizationResult = _commandSecurityManager.Authorize(command);
                    if (!authorizationResult.IsAuthorized)
                    {
                        commandResult.SecurityMessages = authorizationResult.BuildFailedAuthorizationMessages();
                        transaction.Rollback();
                        return(commandResult);
                    }

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

                    if (commandResult.Success)
                    {
                        try
                        {
                            _commandHandlerManager.Handle(command);
                            transaction.Commit();
                        }
                        catch (TargetInvocationException ex)
                        {
                            commandResult.Exception = ex.InnerException;
                            transaction.Rollback();
                        }
                        catch (Exception exception)
                        {
                            commandResult.Exception = exception;
                            transaction.Rollback();
                        }
                    }
                    else
                    {
                        transaction.Rollback();
                    }
                }
            }
            catch (TargetInvocationException ex)
            {
                commandResult.Exception = ex.InnerException;
            }
            catch (Exception ex)
            {
                commandResult.Exception = ex;
            }

            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
        CommandResult Handle(ITransaction transaction, CommandRequest command)
        {
            var commandResult = new CommandResult();

            try
            {
                using (_localizer.BeginScope())
                {
                    _logger.Information("Handle command");

                    commandResult = CommandResult.ForCommand(command);

                    var authorizationResult = _commandSecurityManager.Authorize(command);
                    if (!authorizationResult.IsAuthorized)
                    {
                        commandResult.SecurityMessages = authorizationResult.BuildFailedAuthorizationMessages();
                        transaction.Rollback();
                        return(commandResult);
                    }

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

                    if (commandResult.Success)
                    {
                        try
                        {
                            _commandHandlerManager.Handle(command);
                            transaction.Commit();
                        }
                        catch (TargetInvocationException ex)
                        {
                            _logger.Error(ex, "Error handling command");
                            _exceptionPublisher.Publish(ex);
                            commandResult.Exception = ex.InnerException;
                            transaction.Rollback();
                        }
                        catch (Exception ex)
                        {
                            _logger.Error(ex, "Error handling command");
                            _exceptionPublisher.Publish(ex);
                            commandResult.Exception = ex;
                            transaction.Rollback();
                        }
                    }
                    else
                    {
                        _logger.Information("Command was not successful, rolling back");
                        transaction.Rollback();
                    }
                }
            }
            catch (TargetInvocationException ex)
            {
                _logger.Error(ex, "Error handling command");
                _exceptionPublisher.Publish(ex);
                commandResult.Exception = ex.InnerException;
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "Error handling command");
                _exceptionPublisher.Publish(ex);
                commandResult.Exception = ex;
            }

            return(commandResult);
        }