Thursday, July 8, 2010

Session State for C#


public static class CurrentContext
{
private static T GetFromSession(string key)
{
object obj = HttpContext.Current.Session[key];
if (obj == null)
{
return default(T);
}
return (T)obj;
}

private static void SetInSession(string key, T value)
{
if (value == null)
{
HttpContext.Current.Session.Remove(key);
}
else
{
HttpContext.Current.Session[key] = value;
}
}

private static T GetFromCache(string key)
{
object obj = HttpContext.Current.Cache[key];
if (obj == null)
{
return default(T);
}
return (T)obj;
}

private static void SetInCache(string key, T value)
{
if (value == null)
{
HttpContext.Current.Cache.Remove(key);
}
else
{
HttpContext.Current.Cache[key] = value;
}
}

public static Order CurrentOrder
{
get { return GetFromSession("CurrentOrder"); }
set { SetInSession("CurrentOrder", value); }
}

public static OrderCustomer CurrentOrderCustomer
{
get { return GetFromSession("CurrentOrderCustomer"); }
set { SetInSession("CurrentOrderCustomer", value); }
}

No comments:

Post a Comment