Improving your DWR: Calling events

I’m a big fan of writing less code or even actively deleting existing code.  Less code means less bugs, and who doesn’t like shortcuts to getting around writing mind-numbing, repetitive code?  One place that I find a whole lot of repetition in my code is in calling event delegates.  Before calling an event delegate you always have to make sure it’s not null, and a generally accepted practice is to make a copy of the delegate in case a subscriber unhooks the event after you’ve checked.  So a typical call to raise an event looks like this:

virtual void OnMyEvent(EventArgs args)
{
var handler = MyEvent;
if (handler != null)
return;

handler(this, args);
}


Sure, it’s small enough, but when you have a lot of events in a large solution, you have a whole lot of repetitive code.  Admit it – you’ve used CTRL-C/CTRL-V to copy this for a new event. There’s got to be a better way, right? 


Well how about an extension method (well a pair, since you might have EventHandler<T>)?


public static class EventHandlerExtensions
{
public static void Fire(this EventHandler h, object sender, EventArgs args)
{
var handler = h;
if (handler == null) return;
handler(sender, args);
}

public static void Fire(this EventHandler h, object sender, T args) where T : EventArgs
{
var handler = h;
if (handler == null) return;
handler(sender, args);
}
}

Once you’ve done this your code becomes a simple one-liner:


virtual void OnMyEvent(EventArgs args)
{
MyEvent.Fire(this, args);
}

4 thoughts on “Improving your DWR: Calling events”

  1. Even less code…

    public event EventHandler MyEvent = delegate {}

    public void SomeMethod()
    {

    MyEvent(this, EventArgs.Empty);
    }

    In addition, it works with any type of handler delegate without adding more extensions methods.

    BTW, what happened to Project Resistor?

    Like

  2. Juanma:

    I think I still prefer my version. it’s faster (yes, I know the speed benefit is marginal), it is an easier mechanism to "discover" for other developers on the project since it comes up in intellisense and I don’t have to always check other developer’s code to see if they declared it with a default or not.

    As for Project Resistance (http://resistance.codeplex.com), the download/view rate was really, really low. I took that to mean that it wasn’t of much interest to the community at large so I decided to spend my time working on other things.

    Like

  3. I think you have wrong code:
    var handler = h;
    if (h == null) return;
    h(sender, args);

    must be:
    var handler = h;
    if (handler == null) return;
    handler(sender, args);

    Like

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