事件聚合器:需要帮助修改解决方案
我正在尝试为我的应用程序设计一个事件聚合器。 (我是设计模式的新手,所以我可能还没有完全理解它)。
首先,我已经创建了一个解决方案,它在某种程度上是一个聚合器,但需要改进和重构以实现更高的效率并减少依赖性。
1) 在事件聚合器设计模式中,我的.NET 数据源是否会被视为发布者? (我不确定出版商的角色是什么)。
2)我将如何设计我的解决方案来不仅处理特定数据源的订阅,还处理特定数据事件的订阅,同时忽略其他数据事件?我希望看到一个解决方案,不再保留列表并不知疲倦地循环它们......但不确定我是否可以一起避免这一切。
I'm attempting to design an event aggregator for my application. (I am new to the design pattern, so I may not fully understand it yet).
Firstly, I have created a solution already that is somewhat an aggregator but needs improvement and refactoring to achieve better efficiency and lessen dependencies.
1) In the Event Aggregator design pattern, would my .NET data sources be considered the publishers? (I'm uncertain about the publishers' role is).
2) How would I design my solution to handle subscriptions not only to specific data sources, but also specific data events while ignoring others? I would like to see a solution that moves away from keeping lists and looping through them tirelessly.. but not sure if I can avoid this all together.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你必须在这里实现一个好的发布者/订阅者。我的建议是这样的:
您的发布者:
EventAggregator
应该具有如下方法:您的
IEventObserver
应该看起来像您的订阅者应该实现此接口。
您的
EventFilter
类应该具有您打算使用的所有过滤属性。这个类可以有这样的方法:
创建一个名为
DetailedEventArgs
的类,并从EventArgs
继承它,将您想要的有关事件的所有详细信息放在该类上。在
Register
方法中,您应该存储过滤器和观察者。现在,当您的EventAggregator
捕获事件时,它应该首先从接收到的事件创建DetailedEventArgs
对象,然后循环观察者及其过滤器以查看过滤器是否满足对象,如果是,则使用原始发送者和 DetailedEventArgs 对象调用观察者的 Notify 方法。我强烈建议您检查发布者端而不是订阅者端的过滤器。因为在订阅者端进行检查会导致大量代码重复和不一致。
编辑:示例
EventFilter
和DetailedEventArgs
类:希望我有所帮助:)
I think you have to implement a good publisher/subscriber here. My suggestion is something like this:
your publisher:
EventAggregator
should have methods like:your
IEventObserver
should look likeyour subscribers should implement this interface.
your
EventFilter
class should have all the filtering properties that you intend to use.this class can have a method like this:
and create a class Named
DetailedEventArgs
and inherit it fromEventArgs
, put all the details you want about an event on that.in the
Register
method, you should store both filters and observers. now when yourEventAggregator
catches an event, it should first create theDetailedEventArgs
object from the received event, and then loop the observers and their filters to see if the filter is satisfied by the object and if so, call the Notify method of observer with the original sender andDetailedEventArgs
object.I seriously recommend you to check the filter on publisher side and not the subscriber side. because checking in subscriber side will make lots of code duplicate and inconsistency.
EDIT: An example
EventFilter
andDetailedEventArgs
classes:hope I helped :)
查看 MVVM Light Messenger 类。我的实施基于此。但是,它使用列表和循环,但我没有看到解决方法。
我看到了一些关于内存泄漏的投诉,但这并没有影响我,并且可能会被修复。
Check out the MVVM Light Messenger class. I based my implementation on that. However, it uses lists and loops, but I don't see a way around that.
I have seen some complaints about a memory leak, but it has not impacted me, and may be fixed.