My Strongly Typed Session Wrapper for ASP.NET 2.0
Strongly Typed Session Wrapper in ASP.NET 2.0
Here's my Strongly typed session wrapper in .net 2.0. Its a singleton that interacts with the context object. Its adapted from c# code from Raymond Lewallen. I couldn't get his one to get the theSession = new ApplicationSessionManager(); context.Session["TheSession"] = theSession; line to work correctly after converting to vb.net.... It just kept coming back null. Probably something stupid I did.... So I wrote something of my own based off of his. Unfortunately I was too lazy to do type checking of the session object in the sessionmanager.vb.... That will probably be added later. (There is a case where you could save something to sesssion with the id "thesession" and overwrite this object with a string value without type checking it).
Be sure to add serializable to the class type... I included a complex sub-class for illustration purposes when it comes to instantiating this bad boy.
SessionManager.vb
Imports System.Web
Namespace JSfirm.Session
Public Class SessionManager
Private Const SESSION_MANAGER As String = "SESSION_MANAGER"
Private _Messageinfo As MessageInfo
Private Sub New()
_Messageinfo = New MessageInfo
End Sub
Public Shared ReadOnly Property Instance() As SessionManager
Get
Dim context As HttpContext = HttpContext.Current
Dim manager As SessionManager = CType(context.Session(SESSION_MANAGER), SessionManager)
If manager Is Nothing Then
manager = New SessionManager
context.Session(SESSION_MANAGER) = manager
End If
Return manager
End Get
End Property
Public Sub Abandon()
HttpContext.Current.Session.Abandon()
End Sub
Public Sub Clear()
HttpContext.Current.Session.Clear()
End Sub
#Region "Properties"
Private _clientName As String
Public Property ClientName() As String
Get
Return _clientName
End Get
Set(ByVal value As String)
If value Is Nothing Then
Throw New ArgumentNullException("ClientName", "Cannot store a null value in session.")
End If
_clientName = value
End Set
End Property
Public Property DisplayMessage() As MessageInfo
Get
Return _MessageInfo
End Get
Set(ByVal value As MessageInfo)
_MessageInfo = value
End Set
End Property
Public Class MessageInfo
Private _PageTitle As String
Public Property PageTitle() As String
Get
Return _PageTitle
End Get
Set(ByVal value As String)
_PageTitle = value
End Set
End Property
Private _Headline As String
Public Property Headline() As String
Get
Return _Headline
End Get
Set(ByVal value As String)
_Headline = value
End Set
End Property
Private _Message As String
Public Property Message() As String
Get
Return _Message
End Get
Set(ByVal value As String)
_Message = value
End Set
End Property
Private _RedirectURL As String
Public Property RedirectURL() As String
Get
Return _RedirectURL
End Get
Set(ByVal value As String)
_RedirectURL = value
End Set
End Property
End Class
#End Region
End Class
End Namespace
Here's my basepage to instantiate it.
basepage.vb
Imports Microsoft.VisualBasic
Namespace JSfirm.Basepages
Public MustInherit Class Root
Inherits System.Web.UI.Page
Public MySession As JSfirm.Session.SessionManager
Private Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
MySession = JSfirm.Session.SessionManager.Instance
End Sub
End Class
End Namespace
Then in your Actual pages, you can add & extract data from session in a strongly typed manner as so.
MySession.ClientName = "Allanon of Paranor"
and extract them using the same object semantics. textbox1.text = MySession.ClientName
here is the link to the original c# project this was based off of.