Uso de variables estáticas en lugar del estado de la aplicación en ASP.NET
Planeo usar variables estáticas en lugar del estado de la aplicación en ASP.NET y me pregunto si este es el enfoque correcto:
[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);
}
...
}
Como puede ver, uso el archivo Global.asax (y el código detrás) creado automáticamente. He añadido algunas variables y métodos estáticos. Puedo usarlos después de esta manera:
[some .cs file]
foreach(KeyValuePair<string, object> dictItem in Global.CacheItems)
{
...
¿Es esta la forma correcta o debería crear una nueva clase en lugar de una Global existente? Si debo crear una nueva clase, ¿cómo puedo hacer eso y dónde?