Most often when developing ASP.NET (WebForms or MVC) applications the implementation of session-state is found to be an after-thought whereby such code is thrown in helter-skelter wherever it is found to be necessary. Given that session-state is supposed to be used sparingly the thought that such implementations should be done cleanly is not much a priority. The result is that session-state code is found many times look as if it was just thrown in as a necessity leaving developers who come onto a project later in its development cycle to have to hunt through an application to find where a session-state variable is set and where it is retrieved.
Session-state implementations should be just as cleanly incorporated into an application as any set of functionality and there is a very simple way to do this that not only centralizes all such code but makes it much easier for any developer to understand what variables represent session-state across an entire application.
We do this by setting up an independent class library that can be simply called “SessionState”, which will contain two classes; the first being a structure that will contain all of the necessary session-state variables (or in this case properties) and a second that will process whether such a variable should be placed in a user’s session-state or instead be inserted into application-state.
The first class (which, if desired, can be a structure to make it more lightweight) we will then call, “SessionStateObjects”, and can be set up as follows (with as many properties as necessary)…
Public Class SessionStateObjects ' --- ' NOTE: ' "A" - indicates the property is to access application cache ' "S" - indicates the property is to access session cache ' --- #Region "Class Level Declarations" Private coSessionStateManager As New SessionStateManager() #End Region #Region "Constructor(s)" Public Sub New() End Sub #End Region #Region "Class Level Properties - Session Level Items" Public Property PRODUCT_ID() As String Get If coSessionStateManager.Get_SessionStateItem("S", "gsProductId") Is Nothing Then Return ("") Else Return DirectCast(coSessionStateManager.Get_SessionStateItem("S", "gsProductId"), String) End If End Get Set coSessionStateManager.Save_SessionStateItem("S", "gsProductId", value) End Set End Property #End Region End Class
Notice the following items in the above class. First, the two notes at the top show how you will determine how each variable\property is to be stored in state; it will either be “A” for application level state or “S” for user session-state.
Each property you decide to set up will then access the “SessionStateManager” class that will do the actual work of storing your variable data and retrieving it.
The second class then is called the, “SessionStateManager”, which will do all of the state processing for you and will look as follows…
Imports System.Web Imports System.Text Imports System.Collections Imports System.Configuration Friend Class SessionStateManager #Region "Constructor(s)" Friend Sub New() End Sub #End Region #Region "Save Items To Cache" Friend Function Save_SessionStateItem(psStateType As String, psItemName As String, poObjectItem As Object) As Boolean Try Select Case psStateType.Trim().ToUpper() Case "A" ' application cache HttpContext.Current.Cache(psItemName) = poObjectItem Exit Select Case "S" ' session cache HttpContext.Current.Session(psItemName) = poObjectItem Exit Select End Select Catch loException As Exception Return (False) End Try Return (True) End Function #End Region #Region "Get Items From Cache" Friend Function Get_SessionStateItem(psStateType As String, psItemName As String) As Object Dim loStateObject As Object = Nothing Try Select Case psStateType.Trim().ToUpper() Case "A" ' application cache loStateObject = HttpContext.Current.Cache(psItemName) Exit Select Case "S" ' session cache loStateObject = HttpContext.Current.Session(psItemName) Exit Select End Select Catch loException As Exception Return (Nothing) End Try Return (loStateObject) End Function #End Region #Region "Delete Items From Cache" Friend Function Delete_SessionStateItem(psStateType As String, psItemName As String) As Boolean Try Select Case psStateType.Trim().ToUpper() Case "A" ' application cache HttpContext.Current.Cache.Remove(psItemName) Exit Select Case "S" ' session cache HttpContext.Current.Session.Remove(psItemName) Exit Select End Select Catch loException As Exception Return (False) End Try Return (True) End Function Friend Function Delete_AllSessionStateItems() As Boolean Try HttpContext.Current.Session.RemoveAll() Catch loException As Exception Return (False) End Try Return (True) End Function #End Region End Class
Since the “SessionStateManager” class does not need to be seen by any other class but the publicly declared “SessionStateObjects” class, the manager class can be declared as “Friend” in Visual Basic or “internal” in C#.
This is all there is to it and you will be surprised at how simple and well-organized this makes all such session-state implementations even it does require a little more work on your part. And because the “SessionStateManager” class accesses the standard ASP.NET application and session state processes, you may define how it is stored externally the same way as if you had simply implemented standard session-state code (memory or database option).