instead of doing
session(\"myvar1\") = something
session(\"myvar2\") = something
session(\"myvar3\") = something
session(\"myvar4\") = something
I came up with a solution that avoids certain disadvantages of the other solutions posted by keeping the structure of the Session variables intact. It is simply a type-safe shortcut to get and set Session variables.
It's C#, but I posted some auto-generated VB.NET at the end.
The best solutions I have seen (the accepted answer and the one by TheObjectGuy) require a custom class that is stored in a Session variable, and then is pulled from the Session to access its properties with something like MySessionClass.Current.MyProperty.
The problem with this is that if you are currently using (or may use in the future) something other than an InProc Session-State mode (see https://msdn.microsoft.com/en-us/library/ms178586%28v=vs.140%29.aspx), the whole class would have to go through serialization to access a single property.
Also, that means you lose the IEnumerable and ICollection implementations offered by the actual Session, if you need those. With my solution, you can simply access the actual Session if you need this functionality.
You can easily use these session variables and they are type-safe. It can be used alongside statements like Session["MyProperty"], which will allow for conversion of an existing project one reference at a time. So:
int myInt = (int)Session["MyInt"];
Session["MyInt"] = 3;
becomes:
int myInt = SessionVars.MyInt;
SessionVars.MyInt = 3;
Here is the actual class. The CallerMemberName requires .NET 4.5, but even if you are using an older version you could still manage it by explicitly passing the propertyName. Also, the types of the properties must be nullable to make it act exactly the same as standard Session["MyProp"] calls because a non-set
public static class SessionVars
{
private static T Get2([System.Runtime.CompilerServices.CallerMemberName] string propertyName = "")
{
if (HttpContext.Current.Session[propertyName] == null)
{
return default(T);
}
return (T)HttpContext.Current.Session[propertyName];
}
private static void Set2(T value, [System.Runtime.CompilerServices.CallerMemberName] string propertyName = "")
{
HttpContext.Current.Session[propertyName] = value;
}
public static int MyInt { get { return Get2(); } set { Set2(value); } }
public static bool MyBool { get { return Get2(); } set { Set2(value); } }
public static string MyString { get { return Get2(); } set { Set2(value); } }
}
I even wrote a code snippet to facilitate adding these properties:
SessionVars Property
kevinpo
sv
Adds a property for use in a SessionVars class
Expansion
type
int
property
PropertyName
(); } set { Set2<$type$>(value); } }]]>
I'm a C# guy, so this VB.NET is just auto-converted by http://converter.telerik.com/:
Public NotInheritable Class SessionVars
Private Sub New()
End Sub
Private Shared Function Get2(Of T)( Optional propertyName As String = "") As T
If HttpContext.Current.Session(propertyName) Is Nothing Then
Return Nothing
End If
Return DirectCast(HttpContext.Current.Session(propertyName), T)
End Function
Private Shared Sub Set2(Of T)(value As T, Optional propertyName As String = "")
HttpContext.Current.Session(propertyName) = value
End Sub
Public Shared Property MyInt() As Integer
Get
Return Get2(Of Integer)()
End Get
Set
Set2(Of Integer)(value)
End Set
End Property
Public Shared Property MyBool() As Boolean
Get
Return Get2(Of Boolean)()
End Get
Set
Set2(Of Boolean)(value)
End Set
End Property
Public Shared Property MyString() As String
Get
Return Get2(Of String)()
End Get
Set
Set2(Of String)(value)
End Set
End Property
End Class
'=======================================================
'Service provided by Telerik (www.telerik.com)
'Conversion powered by NRefactory.
'Twitter: @telerik
'Facebook: facebook.com/telerik
'=======================================================