Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it a good idea to create an enum for the key names of session values?

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?

like image 826
Fredou Avatar asked Jan 21 '09 14:01

Fredou


3 Answers

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
like image 168
M4N Avatar answered Oct 14 '22 01:10

M4N


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

like image 36
Otávio Décio Avatar answered Oct 14 '22 02:10

Otávio Décio


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;
like image 24
AnthonyWJones Avatar answered Oct 14 '22 00:10

AnthonyWJones