programming4us
programming4us
WEBSITE

Application Patterns and Tips : Use an Event Broker

- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire
1/12/2012 3:57:51 PM
An event broker is merely an object that acts as middleman for any number of events from any objects to any other object. In a way, this is taking the Observer Pattern  even further.

Here’s a simple event broker implementation:

public class EventBroker
{
Dictionary<string, List<Delegate>> _subscriptions =
new Dictionary<string, List<Delegate>>();

public void Register(string eventId, Delegate method)
{
//associate an event handler for an eventId
List<Delegate> delegates = null;
if (!_subscriptions.TryGetValue(eventId, out delegates))
{
delegates = new List<Delegate>();
_subscriptions[eventId] = delegates;
}
delegates.Add(method);
}

public void Unregister(string eventId, Delegate method)
{
//unassociate a specific event handler method for the eventId
List<Delegate> delegates = null;
if (_subscriptions.TryGetValue(eventId, out delegates))
{
delegates.Remove(method);
if (delegates.Count == 0)
{
_subscriptions.Remove(eventId);
}
}
}

public void OnEvent(string eventId, params object[] args)
{
//call all event handlers for the given eventId
List<Delegate> delegates = null;
if (_subscriptions.TryGetValue(eventId, out delegates))
{
foreach (Delegate del in delegates)
{
if (del.Method != null)
{
if (del.Target != null)
{
del.DynamicInvoke(args);
}
}
}
}
}
}


Usage is very simple: Rather than raising normal .NET events, just call the appropriate methods on the EventBroker. The project contains three user controls: One of them raises the event and the other two listen for it. A form owns the event broker and ties everything together, as the following partial code example shows:

public partial class Form1 : Form
{
//a single event broker to tie all controls together
EventBroker _broker = new EventBroker();

public Form1()
{
InitializeComponent();

myControl11.SetEventBroker(_broker);
myControl21.SetEventBroker(_broker);
myControl31.SetEventBroker(_broker);
}
}
public partial class MyControl1 : UserControl
{
EventBroker _broker;

public MyControl1()
{
InitializeComponent();
}

public void SetEventBroker(EventBroker broker)
{
_broker = broker;
}

//when user clicks button, fire the global event
private void buttonTrigger_Click(object sender, EventArgs e)
{
if (_broker != null)
{
_broker.OnEvent("MyEvent");
}
}
}

public partial class MyControl2 : UserControl
{
EventBroker _broker;

public MyControl2()
{
InitializeComponent();
}

public void SetEventBroker(EventBroker broker)
{
_broker = broker;
_broker.Register("MyEvent", new MethodInvoker(OnMyEvent));
}

private void OnMyEvent()
{
labelResult.Text = "Event triggered!";
}
}
//MyControl3 is the same as MyControl2


See the EventBroker sample for the full source.

Using this method gives you a few advantages:

  • Because strings are used, any component can publish or subscribe to any event without having to add a reference to a strongly typed object.

  • Because no component knows anything about the origin or destination of events, it is trivial to add or remove components with breaking dependencies.

Note

This method is most appropriate for global events that you need to communicate across the entire application, and passing objects around complicated code hierarchies just to listen for events is not worth the headache and maintenance problems that are entailed. For more local events, you should definitely just use the normal .NET event pattern.

Other  
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
REVIEW
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us
programming4us
 
 
programming4us