Are there pitfalls to using static class/event as an application message bus

Posted by Doug Clutter on Stack Overflow See other posts from Stack Overflow or by Doug Clutter
Published on 2010-05-13T19:30:31Z Indexed on 2010/05/13 19:34 UTC
Read the original article Hit count: 266

Filed under:
|
|

I have a static generic class that helps me move events around with very little overhead:

public static class MessageBus<T> where T : EventArgs
{
    public static event EventHandler<T> MessageReceived;
    public static void SendMessage(object sender, T message)
    {
        if (MessageReceived != null)
            MessageReceived(sender, message);
    }
}

To create a system-wide message bus, I simply need to define an EventArgs class to pass around any arbitrary bits of information:

class MyEventArgs : EventArgs
{
    public string Message { get; set; }
}

Anywhere I'm interested in this event, I just wire up a handler:

MessageBus<MyEventArgs>.MessageReceived += (s,e) => DoSomething();

Likewise, triggering the event is just as easy:

MessageBus<MyEventArgs>.SendMessage(this, new MyEventArgs() {Message="hi mom"});

Using MessageBus and a custom EventArgs class lets me have an application wide message sink for a specific type of message. This comes in handy when you have several forms that, for example, display customer information and maybe a couple forms that update that information. None of the forms know about each other and none of them need to be wired to a static "super class".

I have a couple questions:

  1. fxCop complains about using static methods with generics, but this is exactly what I'm after here. I want there to be exactly one MessageBus for each type of message handled. Using a static with a generic saves me from writing all the code that would maintain the list of MessageBus objects.

  2. Are the listening objects being kept "alive" via the MessageReceived event?

For instance, perhaps I have this code in a Form.Load event:

MessageBus<CustomerChangedEventArgs>.MessageReceived += (s,e) => DoReload();

When the Form is Closed, is the Form being retained in memory because MessageReceived has a reference to its DoReload method? Should I be removing the reference when the form closes:

MessageBus<CustomerChangedEventArgs>.MessageReceived -= (s,e) => DoReload();

© Stack Overflow or respective owner

Related posts about c#

Related posts about events