轻量级消息库?

发布于 2024-12-01 07:47:07 字数 494 浏览 6 评论 0原文

我已经使用 Caliburn.MicroEventAggregator 几个星期了,并且非常喜欢它。我现在遇到的问题是我的服务处理 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 技术交流群。

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

发布评论

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

评论(2

七月上 2024-12-08 07:47:07

我建议查看 MVVM Light信使类

例如,

class MyMessageHandler : IHandle<Message>
{
  void Handle( Message msg )
  {
    //Do something
  }
}

您不会执行以下操作

class MyMessageHandler
{

  ctor MyMessageHandler()
  {
    Messager.Default.Register<Message>( this, Handle );
  }

  void Handle( Message msg )
  {
    //Do something
  }
}

任何想要发送消息的类只需调用如下所示的内容

Messanger.Default.Send( new Message( ... ) );

I would recommend looking at MVVM Light's Messenger class.

For example, rather than

class MyMessageHandler : IHandle<Message>
{
  void Handle( Message msg )
  {
    //Do something
  }
}

You would do the following

class MyMessageHandler
{

  ctor MyMessageHandler()
  {
    Messager.Default.Register<Message>( this, Handle );
  }

  void Handle( Message msg )
  {
    //Do something
  }
}

Any class which wants to send messages simply calls something like the following

Messanger.Default.Send( new Message( ... ) );
顾铮苏瑾 2024-12-08 07:47:07

由您决定哪个类实现 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

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