CompositeWPF:EventAggregator - 何时使用?

发布于 2024-07-13 22:29:31 字数 444 浏览 3 评论 0原文

我一直在研究复合应用程序库,它很棒,但我无法决定何时使用 EventAggregator...或者更确切地说 - 何时不使用它。

看看 StockTraderRI 的例子,我更加困惑了。 他们在某些情况下使用 EventAggregator,在其他情况下使用“经典”事件(例如在 IAccountPositionService 接口中)。

我已经决定使用它来与繁重的工作任务进行通信,该任务应该在后台线程上运行。 在本例中,EventAggregator 在幕后提供线程编组,因此我不必对此太担心。 除此之外,我喜欢这种方法提供的解耦。

所以我的问题是:当我开始在应用程序中使用 EventAggregator 时,为什么不将其用于所有自定义事件?

I've been looking in to the Composite Application Library, and it's great, but I'm having trouble deciding when to use the EventAggregator... or rather - when NOT to use it.

Looking at the StockTraderRI example, I'm even more confused. They are using the EventAggregator in some cases, and "classic" events in other cases (in for example the IAccountPositionService interface).

I've already decided to use it for communication with a heavy work task, that should run on a background thread. In this case the EventAggregator offers marshalling of threads behind the scenes, so I don't have to worry much about that. Besides that I like the decoupling this approach offers.

So my question is: When I've started using the EventAggregator in my application, why not use it for all custom events?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

楠木可依 2024-07-20 22:29:31

这是一个很好的问题。 在 Composite WPF (Prism) 中,有 3 种可能的方式在应用程序的各个部分之间进行通信。 一种方法是使用 Commanding,它仅用于将 UI 触发的操作传递到实现该操作的实际代码。 另一种方法是使用共享服务,其中多个部分保存对同一服务(单例)的引用,并以经典方式处理该服务上的各种事件。 对于断开连接和异步通信,正如您已经说过的,最好的方法是使用事件聚合器(它严格遵循 Martin Fowler 的模式)。

现在,何时使用和不使用它:

  1. 当您需要在模块之间进行通信时使用它。 (例如,当任何其他模块创建任务时,任务模块需要得到通知)。
  2. 当同一事件有多个可能的接收者或源时,请使用它。 例如,您有一个对象列表,并且希望在保存或创建该类型的对象时刷新它。 您只需订阅此特定事件,而不是保留对所有打开的编辑/创建屏幕的引用。
  3. 当您只需订阅模型视图呈现器区域中的普通事件时,请勿使用它。 例如,如果您的 Presenter 监听 Model 中的更改(例如 Model 实现了 INotifyPropertyChanged),并且您的 Presenter 需要对此类更改做出反应,那么您的 Presenter 最好直接处理 Model 的 PropertyChanged 事件,而不是通过事件聚合器。 因此,如果发送者和接收者位于同一单元,则无需向整个应用程序“广播”此类事件。

我希望这回答了你的问题。

This is a good question. In Composite WPF (Prism) there are 3 possible ways to communicate between parts of your app. One way is to use Commanding, which is used only to pass UI-triggered actions down the road to the actual code implementing that action. Another way is to use Shared Services, where multiple parts hold a reference to the same Service (Singleton) and they handle various events on that service in the classical way. For disconnected and asynchronous communication, as you already stated, the best way is to use the Event Aggregator (which follows closely Martin Fowler's pattern).

Now, when to and not to use it:

  1. Use it when you need to communicate between modules. (for example, a Task module needs to be notified when a Task is created by any other module).
  2. Use it when you have multiple possible receivers or sources of the same event. For example, you have a list of objects and you want to refresh it whenever an object of that type is saved or created. Instead of holding references to all open edit/create screens, you just subscribe to this specific event.
  3. Don't use it when you only have to subscribe to normal events in the Model View Presenter area. For example, if your presenter listens to changes in the Model (for example the Model implements INotifyPropertyChanged) and your Presenter needs to react on such changes, it's better that your Presenter handles directly the PropertyChanged event of the Model instead of diverting such events through the Event Aggregator. So, if both the sender and receiver are in the same unit, there's no need to "broadcast" such events to the whole application.

I hope this answers your question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文