Event Broker Implementation in C# - Full Source Code and Examples

Here is the full source code for Event Broker Implementation in C#. In zipped file you can find EventBroker with associated tests ans simple sample application that describes how loosely coupled objects can exchange data in any host.

EventBroker Internals 

EventBroker is implemented as Singleton. That ensures that all class instances communicates with one and only one instance of EventBroker. Singleton pattern is implemneted by using private constructor and public instance accessor

private static volatile EventBroker instance;
private static object syncRoot = new Object();
private static Dictionary<string, List<Delegate>> subscriptions;

private EventBroker()
{
}

public static EventBroker Instance
{
   
get
   
{
       
if (instance == null)
        {
           
lock (syncRoot)
           
{
               
if (instance == null)
               
{
                   
instance = new EventBroker();
                   
subscriptions = new Dictionary<string, List<Delegate>>();
               
}
           
}
       
}

       
return instance;

   
}
}

Event Broker holds internal list of subscriptions. Subscriptions are stored as generic Dictionary with string key and list of delegates that points to event handlers.

private static Dictionary<string, List<Delegate>> subscriptions; 

Subscribing To Event

When someone subscribs to event it pass event ID and delegate to Event Broker. Event broker uses EventID to locate generic list of Delegates associated with that key and to add Delegate to the List.

EventBroker.Subscribe("event://EventName", new GlobalEventHandler(Event));

public void Event(object sender, GlobalEventEventArgs e)
{
    //Do some work!
}

It is important to note that event name can be any valid string (It's just a key). We used URI notation to simplify unique naming of events.

Firing an Event

Publisher is component that notifies EventBroker that some event has occured. EventBroker will iterate trough list of delegatesassociated with event nameand invoke each one of them passing event data received from publisher. It will give opportunity to subscribersto act based on received data. Simple scenario is a CustomerList control. Each time user change customer selection, CustomerList can raise event and Event Broker will notify all subscribers. Subscribers are controls that presents differend data associated with selected customer.

publisher.FireEvent(eventname, new GlobalEventEventArgs(expected)); 

 

Published Tuesday, November 20, 2007 3:04 PM by Paki
Filed under: , , ,

Comments

# re: Event Broker Implementation in C# - Full Source Code and Examples

Tuesday, April 29, 2008 5:46 PM by Evg

Hi,

I need to implement event broker pattern, after short googeling i found yor post.

The soruce u wrote is for VS 2008, and i use VS 2005 .NET 2.0 .

what can u suggest me??

Thanks in advance.

Evgeny

# re: Event Broker Implementation in C# - Full Source Code and Examples

Tuesday, April 29, 2008 7:38 PM by admin

Hi Evgeyny,

Create new solution in VS 2005, create new projects with the same names and add existing cs files into new projects. Add references and compile. Solution should build without problem. If you have any issues please post comments here

Thank you!

# re: Event Broker Implementation in C# - Full Source Code and Examples

Wednesday, April 30, 2008 7:00 PM by Paki

Hi Evgeyny, have you had a chance to check out proposed solution?

# re: Event Broker Implementation in C# - Full Source Code and Examples

Thursday, May 01, 2008 12:02 PM by zmajcek

@Evgeyny

Could you pls describe the problem which you are trying to solve using "Event Broker" pattern.

# re: Event Broker Implementation in C# - Full Source Code and Examples

Monday, June 02, 2008 12:48 PM by Mark R

Great example - thanks for posting this. One question, though...

Why have you made all methods on EventBroker static? If you are following the Singleton pattern shouldn't just the 'Instance' method be static and then all the rest be instance methods?

# re: Event Broker Implementation in C# - Full Source Code and Examples

Saturday, June 07, 2008 12:01 PM by Paki

You are right :) I made a mistake by marking all methods static. As you said, in order to follow Singleton pattern all methods should be just public

# re: Event Broker Implementation in C# - Full Source Code and Examples

Tuesday, June 24, 2008 8:28 AM by Mike Brown

You do not have to do the locking and stuff...just initialize the static value in place. The C# specification verifies that the initializer for static member variables will only be called once and that is as part of the Class (not object) initialization.

If you want more control over the order of initialization (e.g. one of your static fields has a reliance on another), you can explicitly declare a static initializer.

Other than that looks good.

# re: Event Broker Implementation in C# - Full Source Code and Examples

Monday, December 01, 2008 9:13 AM by john

Thanks for a great post, it was very useful to me.

I'm having one issue, I added some logic he to Execute function and maybe you can shed some light on my error.

I'm getting an error on this line:

safeInvoker.Invoke(x, new object[] {sender...

that my object cannot be Invoked because it has been disposed. It seems like the "if (ctl.IsDisposed)" is never true even when my form shuts down.

My Execute function:

private static void DynamicInvoke(string id, Delegate x, object sender, EventArgs e)

{

           if (x.Method != null)

           {

               if (x.Target is Control)

               {

                   Control ctl = (Control)x.Target;

                   if (ctl.IsDisposed)

                   {

                       Unsubscribe(id, x);

                       return;

                   }

               }

               if (x.Target == null)

               {

                   Unsubscribe(id, x);

                   return;

               }

               ISynchronizeInvoke safeInvoker = x.Target as ISynchronizeInvoke;

               if ((safeInvoker != null) && (safeInvoker.InvokeRequired))

                   safeInvoker.Invoke(x, new object[] {sender, e });

else

                       x.DynamicInvoke(sender, e);

           }

       }

Thanks,

John

Leave a Comment

(required) 
(required) 
(optional)
(required) 
Powered by Community Server (Commercial Edition), by Telligent Systems