EventAggregator与CompositeCommand [英] EventAggregator vs CompositeCommand

查看:92
本文介绍了EventAggregator与CompositeCommand的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Prism指南中一直努力工作,并认为我对他们的大多数沟通工具有所了解.

I worked my way through the Prism guidance and think I got a grasp of most of their communication vehicles.

命令非常简单,因此很显然DelegateCommand仅用于将视图与其模型连接.

Commanding is very straightforward, so it is clear that the DelegateCommand will be used just to connect the View with its Model.

关于跨模块通信,特别是何时在复合命令上使用EventAggregation,还不太清楚.

It is somewhat less clear, when it comes to cross Module Communication, specifically when to use EventAggregation over Composite Commands.

例如,实际效果是相同的.

The practical effect is the same e.g.

  • 您发布活动->所有订阅者都会收到通知并执行代码以作为响应
  • 您执行复合命令->所有已注册的命令都将被执行,并附带其附加代码

这两种方法都是一劳永逸"的,也就是说,他们不关心触发事件/执行命令后订阅者的任何响应.

Both work along the lines of "fire and forget", that is they don't care about any responses from their subscribers after firing the event/executing the commands.

尽管我知道两者(在幕后)的实现方式都非常不同,但我很难看到用法上的实际差异.

I have trouble seeing a practical difference in usage although I understand that the implementation of both (under the hood) is very different.

那么我们应该考虑它的实际含义-事件吗?那是什么时候发生(事件发生)吗?用户没有像网络请求已完成"那样直接请求?

So should we think of what it actually means - Event? Is that when something happens (an event occurs)? Something the user did not directly request like a "web request completed"?

命令?这是否意味着用户单击了某个内容,然后向我们的应用程序发出了命令,直接请求服务?

And Command? Does that mean a user clicked something and thus issued a command to our application, requesting a service directly?

是吗?或者还有其他方法来确定何时使用这些通信工具中的一种来代替另一种.该指南虽然是我读过的最好的文档之一,但没有给出具体的解释.

Is that it? Or are there other ways to determine when to use one of these communication vehicles over the other. The guidance, although one of the best documentations I read, gives no specific explanation.

因此,我希望参与/使用Prism的人们能够帮助您对此有所了解.

So I hope people involved in/using Prism can help in shedding some light on this.

推荐答案

两者之间有两个主要区别.

There are two primary differences between these two.

  1. 可以执行命令.命令 可以说它是否有效 通过调用执行 Command.RaiseCanExecuteChanged()和 有其CanExecute委托 返回false.如果您考虑 全部保存"的情况 CompositeCommand合成几种 保存"命令,但一个 命令说它不能 执行时,全部保存"按钮将 自动禁用(不错!).
  2. EventAggregator是一个消息传递 模式和命令是一个 指挥模式.虽然 CompositeCommands不是显式的 UI模式,它隐式地是这样 (通常将它们连接到 输入操作,例如单击按钮). EventAggregator不是这种方式- 应用程序的任何部分 有效地提高一个EventAggregator 事件:后台进程, ViewModels等.这是一个 消息传递的中介渠道 在您的应用程序的支持下 对于诸如过滤之类的事情, 后台线程执行等.
  1. CanExecute for Commands. A Command can say whether or not it is valid for execution by calling Command.RaiseCanExecuteChanged() and having its CanExecute delegate return false. If you consider the case of a "Save All" CompositeCommand compositing several "Save" commands, but one of the commands saying that it can't execute, the Save All button will automatically disable (nice!).
  2. EventAggregator is a Messaging pattern and Commands are a Commanding pattern. Although CompositeCommands are not explicitly a UI pattern, it is implicitly so (generally they are hooked up to an input action, like a Button click). EventAggregator is not this way - any part of the application effectively raise an EventAggregator event: background processes, ViewModels, etc. It is a brokered avenue for messaging across your application with support for things like filtering, background thread execution, etc.

希望这有助于解释差异.很难说出何时使用它们,但是通常我会凭经验得出这样的规则:如果是引发事件的用户交互,请使用命令执行其他操作,请使用EventAggregator .

Hope this helps explain the differences. It's more difficult to say when to use each, but generally I use the rule of thumb that if it's user interaction that raises the event, use a command for anything else, use EventAggregator.

希望这会有所帮助.

这篇关于EventAggregator与CompositeCommand的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆