Typed session data in ASP.NET made easier still

Philippe Leybaert is unimpressed with Microsoft’s Web Client Software Factory approach for typed session data and offers his own Typed session data made (very) easy which still seems overkill to me comprising as it does of generics, a delegate a helper class to achieve the desired effect. (Whilst you are there check out his very interesting MVC project for ASP.NET called ProMesh)

The solution which I have been using since my .NET 1.1 days is much simpler still and involves nothing more than creating a plain class with properties for every session variable and a static get accessor that obtains or creates it on the HttpContext similar to a singleton.

Here’s an example with the important Current property (slightly cleaned up and improved for this post ;-)

public class MySession {
  public string Name;
  public int LoginID;
  public int CustomerID;

  public static MySession Current {
    get {
      MySession currentSession = HttpContext.Current.Session["_session"] as MySession;
      if (currentSession == null) {
        currentSession = new MySession();
        HttpContext.Current.Session["_session"] = currentSession;
      }
      return currentSession;
    }
  }
}

Using the session data then simply involves operations like:

MySession.Current.Name = NameTextBox.Text;
NameLabel.Text = MySession.Current.Name;

This solution is a lot clearer however all of these solutions use HttpContext.Session which is actually supposed to be there for compatibility with ASP.

Ideally Microsoft would provide us with an option in web.config whereby we can choose our session class and it would just instantiate and track it as part of the session life-cycle.

[)amien

5 responses

  1. Avatar for Rik Hemsley

    Nice idea, though won't work with bound controls which use session data as parameters to their data source.

    Rik Hemsley 3 August 2007
  2. Avatar for Damien Guard

    While it is true you can't have something like:

    <asp:SessionParameter DefaultValue="-1" Name="CustomerID" SessionField="CustomerID" Type="Int32" />

    There is nothing stopping you doing:

    SqlDataSource1.SelectParameters.Add("CustomerID", MySession.Current.CustomerID);

    In the code-behind.

    Damien Guard 3 August 2007
  3. Avatar for Philippe Leybaert

    Your solution is indeed simpler, but it has some important limitations:

    All fields are stored in the session, even if you don't use them (meaning it is not suitable if you have a lot of session variables).

    There is no way to specify defaults that will be returned when there's nothing in the session.

    All session variables have to be defined in the same class.

    But, indeed, in certain cases your solution is very nice.

    Philippe Leybaert 3 August 2007
  4. Avatar for Damien Guard

    There is nothing actually stopping you from creating more than one of these classes as long as each uses a different dictionary key in the get property.

    Defaults can be handled by specifying them in the MySession class.

    The additional overhead of unused variables could well be an issue if you have a large number of sessions that only use a subset of the session variables.

    Damien Guard 3 August 2007
  5. Avatar for ashis

    Thank it is very easy to understand and workable.

    ashis 29 June 2008