Ejemplo n.º 1
0
        public override T Execute <T>(ICommand <T> command)
        {
            CommandContext context = null;

            if (!AlwaysOpenNew)
            {
                // check whether we can reuse the command context
                var existingCommandContext = Context.CommandContext;
                if ((existingCommandContext != null) && IsFromSameEngine(existingCommandContext))
                {
                    context = existingCommandContext;
                }
            }
            var openNew    = context == null;
            var commandObj = command as ICommand <object>;
            var commandInvocationContext = new CommandInvocationContext(commandObj);

            Context.CommandInvocationContext = commandInvocationContext;

            try
            {
                if (openNew)
                {
                    Log.DebugOpeningNewCommandContext();
                    context = CommandContextFactory.CreateCommandContext();
                }
                else
                {
                    Log.DebugReusingExistingCommandContext();
                }

                Context.CommandContext             = context;
                Context.ProcessEngineConfiguration = ProcessEngineConfiguration;

                // delegate to next interceptor in chain
                return(Next.Execute(command));
            }
            //// 取消catch 方便debug
            catch (System.Exception e)
            {
                commandInvocationContext.TrySetThrowable(e);
                throw;
            }
            finally
            {
                try
                {
                    if (openNew)
                    {
                        Log.ClosingCommandContext();
                        context.Close(commandInvocationContext);
                    }
                    else
                    {
                        commandInvocationContext.Rethrow();
                    }
                }
                finally
                {
                    Context.RemoveCommandInvocationContext();
                    Context.RemoveCommandContext();
                    Context.RemoveProcessEngineConfiguration();
                }
            }
        }