Tuesday, September 27, 2011

Use Static Properties Instead of the Application Object to Store Application State

You should store data in static members of the application class instead of in the Application object. This increases performance because you can access a static variable faster than you can access an item in the Application dictionary. The following is a simplified example.



private static string[] _states[];
private static object _lock = new object();

public static string[] States
{
get {return _states;}
}
public static void PopulateStates()
{
//ensure this is thread safe
if(_states == null)
{
lock(_lock)
{
//populate the states… }
}
}
public void Application_OnStart(object sender, EventArgs e)
{
PopulateStates();
}

1 comment:

  1. Analyse if the lock is needed in the Global asax...

    ReplyDelete