Example #1
0
        protected virtual object Execute(TContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            //创建事件参数对象
            var executingArgs = new CommandExecutorExecutingEventArgs(context);

            //激发“Executing”事件
            this.OnExecuting(executingArgs);

            if (executingArgs.Cancel)
            {
                return(executingArgs.Result);
            }

            //执行命令
            this.OnExecute(context);

            //创建事件参数对象
            var executedArgs = new CommandExecutorExecutedEventArgs(context);

            //激发“Executed”事件
            this.OnExecuted(executedArgs);

            //返回最终的执行结果
            return(context.Result);
        }
Example #2
0
        protected virtual void OnExecuting(CommandExecutorExecutingEventArgs args)
        {
            var executing = this.Executing;

            if (executing != null)
            {
                executing(this, args);
            }
        }
Example #3
0
 protected virtual void OnExecuting(CommandExecutorExecutingEventArgs args)
 {
     this.Executing?.Invoke(this, args);
 }
Example #4
0
        public object Execute(string commandText, object parameter = null)
        {
            if (string.IsNullOrWhiteSpace(commandText))
            {
                throw new ArgumentNullException(nameof(commandText));
            }

            CommandExecutorContext context = null;

            try
            {
                //创建命令执行器上下文对象
                context = this.CreateExecutorContext(commandText, parameter);

                if (context == null)
                {
                    throw new InvalidOperationException("Create executor context failed.");
                }
            }
            catch (Exception ex)
            {
                //激发“Error”事件
                if (!this.OnFailed(context, ex))
                {
                    throw;
                }

                return(null);
            }

            //创建事件参数对象
            var executingArgs = new CommandExecutorExecutingEventArgs(context);

            //激发“Executing”事件
            this.OnExecuting(executingArgs);

            if (executingArgs.Cancel)
            {
                return(executingArgs.Result);
            }

            object result = null;

            try
            {
                //调用执行请求
                result = this.OnExecute(context);
            }
            catch (Exception ex)
            {
                //激发“Error”事件
                if (!this.OnFailed(context, ex))
                {
                    throw;
                }
            }

            //创建事件参数对象
            var executedArgs = new CommandExecutorExecutedEventArgs(context, result);

            //激发“Executed”事件
            this.OnExecuted(executedArgs);

            //返回最终的执行结果
            return(executedArgs.Result);
        }
Example #5
0
        public object Execute(string commandText, object parameter = null)
        {
            if (string.IsNullOrWhiteSpace(commandText))
            {
                throw new ArgumentNullException(nameof(commandText));
            }

            CommandExecutorContext context = null;

            try
            {
                //创建命令执行器上下文对象
                context = this.CreateExecutorContext(commandText, parameter);

                if (context == null)
                {
                    throw new InvalidOperationException("Create executor context failed.");
                }
            }
            catch (Exception ex)
            {
                //激发“Error”事件
                if (!this.OnFailed(context, ex))
                {
                    throw;
                }

                return(null);
            }

            //创建事件参数对象
            var executingArgs = new CommandExecutorExecutingEventArgs(context);

            //激发“Executing”事件
            this.OnExecuting(executingArgs);

            if (executingArgs.Cancel)
            {
                return(executingArgs.Result);
            }

            var complateInvoked = false;
            IEnumerable <CommandCompletionContext> completes = null;

            try
            {
                //调用执行请求
                context.Result = this.OnExecute(context, out completes);
            }
            catch (Exception ex)
            {
                complateInvoked = true;

                //执行命令完成通知
                this.OnCompletes(completes, ex);

                //激发“Error”事件
                if (!this.OnFailed(context, ex))
                {
                    throw;
                }
            }

            //执行命令完成通知
            if (!complateInvoked)
            {
                this.OnCompletes(completes, null);
            }

            //创建事件参数对象
            var executedArgs = new CommandExecutorExecutedEventArgs(context);

            //激发“Executed”事件
            this.OnExecuted(executedArgs);

            //返回最终的执行结果
            return(executedArgs.Result);
        }
		protected virtual void OnExecuting(CommandExecutorExecutingEventArgs args)
		{
			this.Executing?.Invoke(this, args);
		}
		public object Execute(string commandText, object parameter = null)
		{
			if(string.IsNullOrWhiteSpace(commandText))
				throw new ArgumentNullException(nameof(commandText));

			//创建命令执行器上下文对象
			var context = this.CreateExecutorContext(commandText, parameter);

			if(context == null)
				throw new InvalidOperationException("The context of this command executor is null.");

			//创建事件参数对象
			var executingArgs = new CommandExecutorExecutingEventArgs(context);

			//激发“Executing”事件
			this.OnExecuting(executingArgs);

			if(executingArgs.Cancel)
				return executingArgs.Result;

			object result = null;

			try
			{
				//调用执行请求
				result = this.OnExecute(context);
			}
			catch(Exception ex)
			{
				//激发“Error”事件
				if(!this.OnFailed(context, ex))
					throw;
			}

			//创建事件参数对象
			var executedArgs = new CommandExecutorExecutedEventArgs(context, result);

			//激发“Executed”事件
			this.OnExecuted(executedArgs);

			//返回最终的执行结果
			return executedArgs.Result;
		}