轻量级消息库?
我已经使用 Caliburn.Micro
的 EventAggregator
几个星期了,并且非常喜欢它。我现在遇到的问题是我的服务处理 3 种类型的消息。如果每种消息类型需要不同的依赖项,我就必须在构造函数中注入至少 3 个依赖项才能处理消息。我希望我的服务处理消息的唯一时间是当我需要更改 UI 的状态时。
理想情况下,我希望每个处理程序都有自己的类。但是,Caliburn.Micro
提供的 EventAggregator
看起来并没有创建处理程序类的实例。这样,如果每个消息处理程序需要不同的依赖项,那么它就不会打扰我的核心服务。
是否有替代的轻量级接口可以使用 Caliburn.Micro
提供的 IHandle
或类似 ConsumerOf
的接口?由于所有消息传递都将在应用程序内完成,因此我不需要成熟的服务总线。
I've been using the EventAggregator
from Caliburn.Micro
for a few weeks now and really enjoying it. The problem I'm running into now is say my service handles 3 types of messages. If each message type requires a different dependency I'm forced to inject a minimum of 3 dependencies into my constructor just to handle messages. The only time I would want my services to handle a message is when say I need to change the state of the UI.
Ideally, I would like each handler to be its own class. However, it doesn't look like the EventAggregator
provided by Caliburn.Micro
creates instance of handler classes. This way if each message handler requires different dependencies then it won't bother my core services.
Is there an alternative lightweight for using the same interface that Caliburn.Micro
provides IHandle<T>
, or something like ConsumerOf<T>
? Since all messaging will be done within the application I don't need a full fledged service bus.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议查看 MVVM Light信使类。
例如,
您不会执行以下操作
任何想要发送消息的类只需调用如下所示的内容
I would recommend looking at MVVM Light's Messenger class.
For example, rather than
You would do the following
Any class which wants to send messages simply calls something like the following
由您决定哪个类实现 IHandle。您可以选择在单独的类(例如 MessageType1Handler、MessageType2Handler 等)中执行此操作,并将其作为单例添加到您的 DI 容器中?如果您可以提供更多代码(服务类的示例,以及消息发布到/订阅的位置),那就太好了
It's up to you which class implements IHandle. You could choose to do this in separate classes (e.g. MessageType1Handler, MessageType2Handler etc.) and add it as a singleton to your DI container? If you could supply some more code (the example of your service class, and where the message is being published to/subscribed from) that would be great