Beispiel #1
0
        private TCommandResult InternalExecute <TCommand, TCommandResult>(TCommand command)
            where TCommand : ICommand <TCommandResult>
            where TCommandResult : CommandResult, new()
        {
            var result = CreateDefaultCommandResult <TCommand, TCommandResult>(command);

            var handler = _commandHandlerProvider.GetCommandHandler <TCommand, TCommandResult>();

            if (handler != null)
            {
                var hasTransactionAttribute = handler.HasTransactionAttribute();

                if (hasTransactionAttribute)
                {
                    _dbSessionManager.OpenTransaction();
                }

                try
                {
                    var checkResult = _prerequisitesChecker.Check(command);
                    if (!checkResult.ContainsError)
                    {
                        result = handler.Handle(command);
                    }
                    else
                    {
                        result.Message = checkResult.Message;
                    }

                    if (hasTransactionAttribute)
                    {
                        _dbSessionManager.CommitTransaction();
                    }
                }
                catch (Exception)
                {
                    if (hasTransactionAttribute)
                    {
                        _dbSessionManager.RollbackTransaction();
                    }
                    throw;
                }
            }

            return(result);
        }
Beispiel #2
0
        public CommandResult Execute <TCommand>(TCommand command)
        {
            var result = new CommandResult
            {
                ContainsError = true,
                Message       = String.Format("Handler not found for command {0}", command.GetType().Name)
            };

            var handler = _commandHandlerProvider.GetCommandHandler <TCommand>();

            if (handler != null)
            {
                var hasTransactionAttribute = handler.HasTransactionAttribute();

                if (hasTransactionAttribute)
                {
                    _dbSessionManager.OpenTransaction();
                }

                try
                {
                    var checkResult = _prerequisitesChecker.Check(command);
                    if (!checkResult.ContainsError)
                    {
                        result = handler.Handle(command);
                    }
                    else
                    {
                        result.Message = checkResult.Message;
                    }

                    if (hasTransactionAttribute)
                    {
                        _dbSessionManager.CommitTransaction();
                    }
                }
                catch (Exception)
                {
                    if (hasTransactionAttribute)
                    {
                        _dbSessionManager.RollbackTransaction();
                    }
                    throw;
                }
            }


            return(result);
        }