Gal Ratner
Gal Ratner is a Techie who lives and works in Los Angeles CA and Austin TX. Follow galratner on Twitter Google
Delayed Reaction - Loosely coupled events Library

While I was working with MVC I found that sometimes I needed to "hack" the model. I needed events from one controller to invoke methods in another controller. yes i know it shouldn’t be done and changes model integrity but still, I had a tree control on one side of the screen and selecting a node should change the data in a table on the other side of the screen. The problem I was facing was how to make components listen to each other when they are clearly part of separate views.

Delayed reaction is a small library I wrote to deal with such issues. You can simply register for events, no matter where they are coming from, and fire your own. Events can be direct - meaning they will fire immediately, Delayed - meaning they will fire after a pause, or Sticky and file in an interval until you stop them.

This concept is also referred to as loosely coupled events.

Working with the library is easy. If you fire an event you simply need to call the FireEvent method with the appropriate parameters.

If you are listening to events you should implement the interface IDelayedReactionListener and the only method there OnRaiseCustomEvent.

To listen to an event use

   22 public partial class Window1 : Window, IDelayedReactionListener

   23     {

   24         public Window1()

   25         {

   26             InitializeComponent();

   27             DelayedReaction.EventManager.Instance.AddMe("Sticky Event", "Events", this);

   28             DelayedReaction.EventManager.Instance.AddMe("Direct Event", "Events", this);

   29             DelayedReaction.EventManager.Instance.AddMe("Delayed Event", "Events", this);

   30         }

 implementing OnRaiseCustomEvent Looks like:

   52 public void OnRaiseCustomEvent(DelayedEventArgs e)

   53         {

   54             MessageBox.Show(e.EventData.ToString());

   55         }

 

Firing and canceling events simply looks like:

   32  private void DirectEventButton_Click(object sender, RoutedEventArgs e)

   33         {

   34             DelayedReaction.EventManager.Instance.FireEvent("Direct Event", "Events", EventDelay.None, 0, EventScope.Internal, this, "Some Event Data");

   35         }

   36 

   37         private void DelayEventButton_Click(object sender, RoutedEventArgs e)

   38         {

   39             DelayedReaction.EventManager.Instance.FireEvent("Delayed Event", "Events", EventDelay.Delayed, 5000, EventScope.Internal, this, "Some Event Data");

   40         }

   41 

   42         private void StickyEventButton_Click(object sender, RoutedEventArgs e)

   43         {

   44             DelayedReaction.EventManager.Instance.FireEvent("Sticky Event", "Events", EventDelay.Sticky, 5000, EventScope.Internal, this, "Some Event Data");

   45         }

   46 

   47         private void CancelStickyEventButton_Click(object sender, RoutedEventArgs e)

   48         {

   49             DelayedReaction.EventManager.Instance.StopStickyEvent("Sticky Event", "Events");

   50         }

 

 

Pretty simple and lets Delayed Reaction take care of cross events for you.

You can download Delayed Reaction from codepex here: http://delayedreaction.codeplex.com/


Posted 21 May 2009 5:46 AM by Gal Ratner
Filed under: ,

Powered by Community Server (Non-Commercial Edition), by Telligent Systems