Introduction
This code is just to:
- Keep all of your Session management in one place.
- Allow you to remember what your Session variables are.
- Simplify code.
Using the code
The normal way to access the Session in a page is like this. Note that there is no intellisense, and that you will need to cast it every time you want to get it.
Week week = (Week) Session["Week"];
Here's what I'm changing it to. Note that intellisense is available to see what variables are kept in the Session.
Week week = SessionStateSink.Week;
Plant plant = SessionStateSink.Plant;
Here is the (static) Session State Sink:
using System.Web.SessionState;
public static class SessionStateSink {
private static HttpSessionState Session{
get {return HttpContext.Current.Session; }
}
public static Week Week {
get {
if (Session["Week"] == null) {
Session.Add("Week",new Week());
}
return (Week)Session["Week"];
}
}
public static Plant Plant {
get {
return (Plant)Session["Plant"];
}
set{
if (Session["Plant"] == null)
Session.Add("Plant",value);
else
Session["Plant"] = value;
}
}
}
Overall, this mostly just makes the code a little cleaner and easier to manage.