Session Management Asp.Net

Please do comment if you find this infromation contradictory.
  • We should always have a centralized location to manage all the session variables in our application. This way, it is really very easy for all the programmers working on the application to get to know about Session Information. This session information can be reused whereever needed.
  • It's not a problem to store "objects" (as opposed to value-types like int) into Session. You could even say it's better to store objects than value-types, because those value-types need "boxing" and "unboxing".
To achieve this purpose, we have used hashtables to be saved in a session. Every module can have its own hashtable to group all the common session information. The key/value pair of the hash can be exposed through public properties. For instance, below is the complete code of Session Management Class for only one variable in a hashtable.

Sample VB.Net Code


1:    Public Shared Property NHPActiveSearchText() As String
  
2:      Get  
3:        If NHPSessionHash("ActiveSearchText") Is Nothing Then  
4:          Return ""  
5:        Else  
6:          Return NHPSessionHash("ActiveSearchText")  
7:        End If  
8:      End Get  
9:      Set(ByVal value As String)  
10:        NHPSessionHash("ActiveSearchText") = value  
11:      End Set  
12:    End Property
  
13:    Public Shared Property NHPSessionHash() As Hashtable  
14:      Get  
15:        Return GetCurrentUserSessionVariable("NHPSessionHash")  
16:      End Get
  
17:      Set(ByVal value As Hashtable)  
18:        SetCurrentUserSessionVariable("NHPSessionHash", value)  
19:      End Set  
20:    End Property
  
21:    Private Shared Function GetCurrentUserSessionVariable(ByVal poKey As Object) As Object  
22:      Dim l_oHttpContext As HttpContext = GetCurrentUserContext()  
23:      If l_oHttpContext Is Nothing Or l_oHttpContext.Session Is Nothing Then  
24:        Throw New ApplicationException("Error getting user district; heck setup and app database connections in the setup DB with TC2.")  
25:      End If  
26:      Return l_oHttpContext.Session(poKey)  
27:    End Function
  
28:    Private Shared Sub SetCurrentUserSessionVariable(ByVal poKey As Object, ByVal poValue As Object)  
29:      Dim l_oHttpContext As HttpContext = GetCurrentUserContext()  
30:      l_oHttpContext.Session(poKey) = poValue  
31:    End Sub
  
32:     Private Shared Function GetCurrentUserContext() As HttpContext  
33:      Return HttpContext.Current  
34:    End Function  

Comments

Popular posts from this blog

Export Data from AX 2012 - using DIXF (Data Migration Framework)

The virtual path maps to another application, which is not allowed

Agile Management - TFS (Priority, Stack Rank and Severity)