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); }
protected virtual void OnExecuting(CommandExecutorExecutingEventArgs args) { this.Executing?.Invoke(this, args); }