views:

442

answers:

6

instead of doing

 session("myvar1") = something
 session("myvar2") = something
 session("myvar3") = something
 session("myvar4") = something

is doing

enum sessionVar
   myvar1
   myvar2
   myvar3
   myvar4
end enum


 session(sessionVar.myvar1.tostring) = something
 session(sessionVar.myvar2.tostring) = something
 session(sessionVar.myvar3.tostring) = something
 session(sessionVar.myvar4.tostring) = something

would be better?

+9  A: 

Only if those values are related. Otherwise use plain old constants.

Otávio Décio
I'm hoping for a type-safe Session in the next ASP.Net
Joel Coehoorn
constant would remove the need of .toString, which is very good
Fredou
@Joel: look at my answer for my approach
M4N
+3  A: 

How about:-

public static class  SessionVar
{
  public static readonly string myVar1 = "myVar1";
  public static readonly string myVar2 = "myVar2";
  public static readonly string myVar3 = "myVar3";
  public static readonly string myVar4 = "myVar4";
}

This allows you to use:-

session(SessionVar.myVar1) = something;
AnthonyWJones
+11  A: 

Instead of using constants for the session keys, I'm using my own type-safe session object, which looks like this (sorry this is in C#, see below for a VB version):

public class MySession
{
  // Private constructor (use MySession.Current to access the current instance).
  private MySession() {}

  // Gets the current session.
  public static MySession Current
  {
    get
    {
      MySession session = HttpContext.Current.Session["__MySession__"] as MySession;
      if (session == null)
      {
        session = new MySession();
        HttpContext.Current.Session["__MySession__"] = session;
      }
      return session;
    }
  }

  // My session data goes here:
  public string MyString { get; set; };
  public bool MyFlag { get; set; };
  public int MyNumber { get; set; };
}

Whenever I need to read/write something to/from the session, I can use my typesafe session object like this:

string s = MySession.Current.MyString;
s = "new value";
MySession.Current.MyString = s;

This solution results in several advantages:

  • I have a typesafe Session (no more type-casts)
  • I can document all session based data (by commenting the public properties in MySession)
  • When adding a new element to the session, I don't have to search the solution to check if the same session-key was already used somewhere else.

Update: Here's a VB version (automatically converted from the C# version). Sorry, but I don't know VB and so I didn't know how to write the properties in VB:

Public Class MySession
    ' Private constructor (use MySession.Current to access the current instance).
    Private Sub New()
    End Sub

    ' Gets the current session.
    Public Shared ReadOnly Property Current() As MySession
     Get
      Dim session As MySession = TryCast(HttpContext.Current.Session("__MySession__"), MySession)
      If session = Nothing Then
       session = New MySession()
       HttpContext.Current.Session("__MySession__") = session
      End If
      Return session
     End Get
    End Property

    ' My session data goes here:
    Public MyString As String
    Public MyFlag As Boolean
    Public MyNumber As Integer
End Class
M4N
This is clean and simple. I like it.
EnocNRoll
Perhaps post a VB version for Fredou.
EnocNRoll
The downside of this approach is that you do not offer an IEnumerable implementation, but one could argue that it should not be needed except for complex types that are stored as lists of some sort.
EnocNRoll
interesting one, this would remove a lot of re-casting and make the code a lot cleaner. I'm going to try it and I will probably mark this as the answer. Back in a few minutes.
Fredou
A: 

For the simple thing of removing all of the string key references, I would go with global static/shared constants visible at application scope.

Otherwise, the strongly typed simple wrapper for session variables is a superior alternative, and it's far more OOP-friendly considering that you get intellisense and Object Browser compatibility.

The only way I can see the enum being of much value is to be used to index into an array or a similar list. But even then you have to cast the enum to an int.

So, you could have an array that you load at application startup with all of your session variable keys and then an enum for the indexes. However, given that the Session object derives from HttpSessionState, which derives from IEnumerable, you should be able to just do a foreach loop over the session variables, if you need to.

EnocNRoll
A: 

I've used classes like this to make a typed session/cache wrapper. You may need to add additional code to the get/set, but I'll leave that up to you.

internal class SessionHelper
{
    private const string  myVar1Key = "myvar1";

    public static int MyVar1
    {
     get
     {
      return (int)System.Web.HttpContext.Current.Session[myVar1Key];
     }
     set
     {
      System.Web.HttpContext.Current.Session[myVar1Key] = value;
     }
    }
}

Sorry about the C#....

Greg
A: 

I realize this question was asked a while ago and an "answer" has already been picked. But I just came across it. Martin's answer is good. However, to assist anyone who stumbles across this in the future, if you really want a slick way to deal with Session, read this post. I don't think you'll find anything easier.

TheObjectGuy