views:

629

answers:

5

Hello,

I want to make a very simple event bus which will allow any client to subscribe to a particular type of event and when any publisher pushes an event on the bus using EventBus.PushEvent() method only the clients that subscribed to that particular event type will get the event.

I am using c#.net 2.0

Any help/pointer would be greatly appreciated.

Thanks Pradeep

A: 

You should check out episode 3 in Hibernating Rhinos, Ayende's screen casts series - "Implementing the event broker".

It shows how you can implement a very simple event broker using Windsor to wire things up. Source code is included as well.

The proposed event broker solution is very simple, but it would not take too many hours to augment the solution to allow arguments to be passed along with the events.

mookid8000
+1  A: 

The Composite Application Block includes an event broker that might be of use to you.

Simon
A: 

I found this.

http://perrybirch.blogspot.com/2007/06/generic-message-bus.html

Simple one class.

Thanks folks.

pradeep

chikak
+1  A: 

You might also check out Unity extensions: http://msdn.microsoft.com/en-us/library/cc440958.aspx

[Publishes("TimerTick")]
public event EventHandler Expired;
private void OnTick(Object sender, EventArgs e)
{
  timer.Stop();
  OnExpired(this);
}

[SubscribesTo("TimerTick")]
public void OnTimerExpired(Object sender, EventArgs e)
{
  EventHandler handlers = ChangeLight;
  if(handlers != null)
  {
    handlers(this, EventArgs.Empty);
  }
  currentLight = ( currentLight + 1 ) % 3;
  timer.Duration = lightTimes[currentLight];
  timer.Start();
}

Are there better ones?

Volker von Einem
+1  A: 

Another good implementation can be found at:

http://code.google.com/p/fracture/source/browse/trunk/Squared/Util/EventBus.cs

Use cases is accessible at: /trunk/Squared/Util/UtilTests/Tests/EventTests.cs

This implementation does not need external library.

An improvement may be to be able to subscribe with a type and not a string.

heralight