Handle Your Events - Perform Better
(
Apr 24 2007 - 09:56:03 PM by
Timothy Khouri) - [
print article]
A Quick Overview
If you've ever looked into way Controls are done in ASP.NET by the actual .NET team you'll see some smart performance boosts that us foolish mortals (people not on the .NET team) may not be aware of. One of those techniques is to do your own "Event Accessors" which can really help keep your memory usage down on your web applications. There are a lot of benefits of doing this very simple thing.
If you are making your own custom control or even a custom class such as a BLL (Business Logic Layer) or DAL (Data Access Layer) and you are going to make your own custom events then there are a few things you are going to want to know. First of all, every event that you make is going to create a EventHandlerList behind the scenes to add and remove delegates that people create when they register with your event with a += (or -= to unregister).
What Does That Mean?
There isn't a huge issue here, but if you have multiple events, then you're eating up more and more ram with each EventHandlerList object that is created. Every web control that I have looked into in the .NET Framework (meaning the GridView, Page, TextBox, DropDownList, etc.) all handle their own event accessors by calling the add and remove code themselves. They utilize the "Events" property which is on all objects that inherit from the base "Control" class (which you would too if you were making your own custom control).
You can utilize the "Events" property if you inherit from Control by doing the following:
public class MyCustomControl : Control
{
public event EventHandler MyCustomEvent
{
add
{
base.Events.AddHandler("MyCustomEvent", value);
}
remove
{
base.Events.RemoveHandler("MyCustomEvent", value);
}
}
}
So a very small amount of code can really improve the performance of your custom controls. But what if you wanted to do this with a custom class that isn't inheriting from "Control" or "WebControl" such as a BLL, DAL, Subject Layer or whatever? Well, thankfully that's pretty simple too. The EventHandlerList class isn't tied to ASP.NET controls only. In fact, it's part of the System.ComponentModel namespace, meaning it can be used anywhere (including Windows or Console applications). So if we make a slight modification to the code above by creating our own EventHandlerList instance, we can use it in just the same way that we would if we were making a custom control.
using System.ComponentModel;
public class MyCustomClass
{
private EventHandlerList Events = new EventHandlerList();
public event EventHandler MyCustomEvent
{
add
{
this.Events.AddHandler("MyCustomEvent", value);
}
remove
{
this.Events.RemoveHandler("MyCustomEvent", value);
}
}
}
You can even go one step further than this if you really want to by making your event object static and using a private instance object as the key for the AddHandler and RemoveHandler. This way if you have 100 number people hitting your web application you only won't have 100 EventHandlerLists being created and destroyed all the time. Here is how you would do that:
using System.ComponentModel;
public class MyCustomClass
{
private static EventHandlerList Events = new EventHandlerList();
private object myCustomEventKeyObject;
public event EventHandler MyCustomEvent
{
add
{
lock (MyCustomClass.Events)
{
MyCustomClass.Events.AddHandler(this.myCustomEventKeyObject, value);
}
}
remove
{
lock (MyCustomClass.Events)
{
MyCustomClass.Events.RemoveHandler(this.myCustomEventKeyObject, value);
}
}
}
}
That's pretty much it. There are many more ways to improve the performance of your web applications beyond this which I will go into eventually. This one in particular used to only work in C#, but now even VB.NET can create event accessors.