I plane to use static variables instead of Application state in ASP.NET and am wondering if this is correct approach:
[Global.asax.cs]
...
public class Global : System.Web.HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
}
...
private static Dictionary<string, object> cacheItems = new Dictionary<string, object>();
private static object locker = new object();
public static Dictionary<string, object> CacheItems
{
get
{
lock (locker)
{
return cacheItems;
}
}
set
{
lock (locker)
{
cacheItems = value;
}
}
}
public static void RemoveCacheItem(string key)
{
cacheItems.Remove(key);
}
...
}
As you can see I use automatically created Global.asax (and code behind) file. I've added some static variables and methods. I can use them after in this manner:
[some .cs file]
foreach(KeyValuePair<string, object> dictItem in Global.CacheItems)
{
...
Is this the right way or I should create new class instead of existing Global? If I should create new class how can I do that and where?