CompositeWPF:EventAggregator - 何时使用? [英] CompositeWPF: EventAggregator - when to use?

查看:144
本文介绍了CompositeWPF: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.

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

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).

我已经决定使用它与一个沉重的工作任务通信,应该在后台线程上运行。在这种情况下,EventAggregator在幕后提供线程编组,所以我不用担心。此外,我喜欢这种方法提供的解耦。

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.

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

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

推荐答案

这是一个很好的问题。在复合WPF(Prism)中,有3种可能的方式在应用程序的各个部分之间进行通信。一种方法是使用Commanding,它仅用于将UI触发的操作传递给实现该操作的实际代码。另一种方式是使用共享服务,其中多个部分持有对同一个服务(Singleton)的引用,并且它们以古典的方式处理该服务上的各种事件。对于断开连接和异步通信,如您所说,最好的方法是使用事件聚合器(紧跟Martin Fowler的模式)。

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).

现在,何时使用它:


  1. 当您需要在模块之间进行通信时使用它。 (例如,当任何其他模块创建任务时,需要通知任务模块)。

  2. 当您有多个可能的接收者或相同事件的来源时,请使用它。例如,您有一个对象列表,并且每当保存或创建该类型的对象时,您都要刷新它。不要对所有打开的编辑/创建屏幕进行引用,您只需订阅此特定事件。

  3. 当您只需要在模型视图演示者中订阅普通事件时,不要使用它区。例如,如果您的演示者监听模型中的更改(例如,模型实现了INotifyPropertyChanged),并且您的Presenter需要对此类更改做出反应,则Presenter会直接处理ModelChange的PropertyChanged事件,而不是将这些事件转移到事件聚合器因此,如果发件人和接收者都在同一个单位,则不需要将这些事件广播到整个应用程序。

我希望这回答你的问题。

I hope this answers your question.

这篇关于CompositeWPF:EventAggregator - 何时使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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