Loofer
Loofer

Reputation: 6965

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'. Can you assist me in working out what changes to my code I will need, and what the installer(s) will look like?

Upvotes: 0

Views: 392

Answers (1)

dbones
dbones

Reputation: 4504

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.

Castle has a powerful mechanism which will register types based on convention, Docs here. 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

Hope this helps

Upvotes: 1

Related Questions