What do you think of this code? Essentially I can do things like this:
SessionItem<int> myId= new SessionItem<int>("myId");
if (myId.Exists)
{
user = new User(myId.Value);
}
and
myId.Value = int.Parse(Grid.DataKeys[row.RowIndex].Value.ToString());
for session objects. Code is:
using System;
using System.Web;
namespace Utils
{
public class SessionItem<T>
{
private string key = string.Empty;
public SessionItem (string key)
{
this.key = key;
}
public bool Exists
{
get
{
object value = HttpContext.Current.Session[key];
return value != null;
}
}
public T Value
{
get
{
try
{
object value = HttpContext.Current.Session[key];
return (T)value;
}
catch
{
throw new Exception(string.Format("No session value with a key \"{0}\" exists.", key));
}
}
set
{
HttpContext.Current.Session[key] = value;
}
}
}
}