/// <summary> /// 以异步的方式调用一个命令模型,并返回命令模型执行的值的异步操作。 /// </summary> /// <typeparam name="TResult">返回值的数据类型。</typeparam> /// <param name="bus">命令总线。</param> /// <param name="command">命令模型。</param> /// <param name="executing">命令模型执行前发生的方法。</param> /// <param name="executed">命令模型执行后发生的方法。</param> /// <returns>异步操作。</returns> public static Task <TResult> CallAsync <TResult>(this ICommandBus bus, ICommand <TResult> command , CommandExecutingHandler <ICommand <TResult> > executing = null , CommandExecutedHandler <ICommand <TResult> > executed = null) { if (bus == null) { throw new ArgumentNullException(nameof(bus)); } return((Task <TResult>)GetInvoker(command).Item2(null, bus, command, executing, executed)); }
public static Task <TResult> ExecuteAsync(ICommandBus bus, TCommand command , CommandExecutingHandler <ICommand <TResult> > executing , CommandExecutedHandler <ICommand <TResult> > executed) { CommandExecutingHandler <TCommand> executing2 = null; CommandExecutedHandler <TCommand> executed2 = null; if (executing != null) { executing2 = (a, b) => executing(a, b); } if (executed != null) { executed2 = (a, b, c) => executed(a, b, c); } return(bus.ExecuteAsync(command, executing2, executed2).ContinueWith(t => t.Result.Result)); }
public static TResult Execute(ICommandBus bus, TCommand command , CommandExecutingHandler <ICommand <TResult> > executing , CommandExecutedHandler <ICommand <TResult> > executed) { CommandExecutingHandler <TCommand> executing2 = null; CommandExecutedHandler <TCommand> executed2 = null; if (executing != null) { executing2 = (a, b) => executing(a, b); } if (executed != null) { executed2 = (a, b, c) => executed(a, b, c); } return(bus.Execute(command, executing2, executed2).Result); }
/// <summary> /// 保存Model的命令处理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected virtual void SaveModelCommand(object sender, CommandEventArgs e) { Trace.Write("SaveModelCommad"); if (this.ViewMode != Mode.New && ModelEntity == null) { RetrieveEntity(); } //从控件中获取Model的值 View2Model(); bool success = false; using (Session session = new Session()) { try { if (this.ViewMode == Mode.New) { success = EntityManager.Create(session, ModelEntity); } else { success = EntityManager.Update(session, ModelEntity); } CommandExecutedHandler saveSuccess = this.Events[objAfterSaveModelEventStub] as CommandExecutedHandler; if (saveSuccess != null) { saveSuccess(sender, new ExcutedCmdEventArgs(session, e, success)); } } catch (Exception ex) { CommandExecutedHandler saveFail = this.Events[objAfterSaveModelEventStub] as CommandExecutedHandler; if (saveFail != null) { saveFail(sender, new ExcutedCmdEventArgs(session, e, false, ex)); } } } }
/// <summary> /// 执行一个命令模型。 /// </summary> /// <typeparam name="TCommand">命令模型的数据类型。</typeparam> /// <param name="command">命令模型。</param> /// <param name="executing">命令模型执行前发生的方法。</param> /// <param name="executed">命令模型执行后发生的方法。</param> /// <returns>返回命令模型。</returns> protected virtual TCommand Execute <TCommand>(TCommand command , CommandExecutingHandler <TCommand> executing = null , CommandExecutedHandler <TCommand> executed = null) where TCommand : ICommand { return(this.Bus.Execute(command, executing, executed)); }