简体   繁体   English

与温莎城堡一起实施“综合模式”

[英]Implementing the “Composite pattern” with Castle Windsor

I have an interface 我有一个界面

  public interface IMessageHandler
  {
     void ProcessMessage(CanonicalModelEntityMessage message); 
  }

I have some concrete handlers with this sort of pattern 我有一些具有这种模式的具体处理程序

public class ThingMessageHandler : IMessageHandler
{
    public void ProcessMessage(Message queueMessage){
      //HandleMessage
   }
}

I also have a 'composite' object which will 我也有一个“复合”对象

public class MessageHandler : IMessageHandler
{
    private List<IMessageHandler> _handlers;
    public MessageHandler()
    {
        _handlers =new List<IMessageHandler>();
    }
    public void Handle(CanonicalModelEntityMessage message)
    {
        foreach (var messageHandler in _handlers)
        {
            messageHandler.Handle(message);
        }
    }

    public void Add(IMessageHandler messageHandler)
    {
        _handlers.Add(messageHandler);
    }
}

Each handler gets to see every message. 每个处理程序都可以查看每条消息。

I believe there is a way of wiring this up with Castle, so when more handlers are added it will 'just work'. 我相信可以通过某种方式与Castle进行连接,因此,当添加更多处理程序时,它将“起作用”。 Can you assist me in working out what changes to my code I will need, and what the installer(s) will look like? 您能协助我确定对我的代码进行哪些更改以及安装程序的外观吗?

it sounds like you are trying to do an event publisher / aggregator. 听起来您正在尝试做一个事件发布者/聚合者。

there are a couple of implementations of these on the net, here is an artile: 网上有几种实现方式,下面是一个框架:

Event publisher 活动发布者

  • code at the bottom of the article. 本文底部的代码。 The code is for 2.5, with a fix for 3.1+ in the comments. 该代码适用于2.5,注释中包含针对3.1+的修复程序。

Castle has a powerful mechanism which will register types based on convention, Docs here . Castle具有强大的机制,可以根据约定在Docs此处注册类型。 the above article makes use of this, to auto wire any type which implements the required interface. 上面的文章利用这个来自动连接实现所需接口的任何类型。

Also, a side note: If you want to inject all classes which implement a contract (interface) you will have to register the behaviour with castle windsor, Example of registering a sub resolver and more information can be found enter link description here 另外,请注意:如果要注入实现合同(接口)的所有类,则必须向Castle Windsor注册行为,注册子解析器的示例以及更多信息,请在此处输入链接描述

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM