Windows Forms Eventing: Adding Pub/Sub

In my lastpost, I described the creeping problem I’ve been having with wiring my large WindowsForms application up with EventHandler delegates. In short, the design becomes brittle—it’shard to write and hard to change. Luckily, the solution is an easy one: Publish/Subscribe.

The Event Aggregator is a singleton. You can either manage this yourself, or you canhave your Inversion of Control container do it for you. When an Event Receiver iscreated, it registers itself with the Event Aggregator, which stores a reference tothe receiver. When a Event Sender sends a message to the Event Aggregator, the aggregatorloops through all the registered receivers and calls a method on them to handle theevent. This is facilitated by having the receivers implementing an interface. Here’sa simple implementation:

public class EventMessage{public string MessageText{ get; set; }}public interface IEventReceiver{void Handle(EventMessage message);}public interface IEventAggregator{void SendMessage(EventMessage message);void AddReceiver(IEventReceiver receiver);void RemoveReceiver(IEventReceiver receiver);}public class EventAggregator: IEventAggregator{private readonly List<IEventReceiver>receivers = new List<IEventReceiver>();#region IEventAggregator Memberspublic void SendMessage(EventMessagemessage){receivers.ForEach(r => r.Handle(message));}public void AddReceiver(IEventReceiverreceiver){if (receivers.Contains(receiver)) return;receivers.Add(receiver);}public void RemoveReceiver(IEventReceiverreceiver){receivers.Remove(receiver);}#endregion}public class EventReceiver: IEventReceiver{public EventReceiver(IEventAggregator aggregator){aggregator.AddReceiver(this);}#region IEventReceiver Memberspublic void Handle(EventMessagemessage){Console.WriteLine(message.MessageText);}#endregion}public class EventSender{private IEventAggregator aggregator;public EventSender(IEventAggregator aggregator){this.aggregator = aggregator;}public void SendErrorMessage(string message){aggregator.SendMessage(new EventMessage { MessageText= message });}}

This is a big improvement over manual event wiring through an application. No longerdo the targets of a message need to know (have references to) the senders of the messagein order to make the connection. You can add a new target (receiver) without changingthe code of the sender. You can add a new sender without changing the code of thereceivers. And you don’t have to manually map things out in some sort of registryclass. This is simple easy and it will work.

It’s always something with you, isn’t it?

Ok. I like it. I’m happy. But how many of these things am I going to have floatingaround? It seems like a lot of code to write to hand one type of event between somecomponents. I’m going to have to hire some Indian outsourcing company to write allof these singleton aggregators.

This sounds like a job for generics! My next post will rewrite the Event Aggregatorso that a single aggregator can handle every message your application has.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s