Shared Session Data

M

MS ASP.NET

I'm using sql server session state and wrote a class to wrap current
session. I need to know if what I am doing will result in users
sharing session data.

'----------------------------------
'------- Session Wrapper --------
'-----------------------------------
Imports System.Web.SessionState
Public Class EmployeeSession

Private _session As HttpSessionState

Sub New()
_session = HttpContext.Current.Session
End Sub

Property EmployeeID() As Integer
Get
Return _session("EmployeeID")
End Get
Set(ByVal Value As Integer)
_session("EmployeeID") = Value
End Set
End Property

End Class
'-----------------------------------


'-----------------------------------
'-------- Sub on a web form -------
'-----------------------------------

Private Sub DoSomething
Dim empSession as new EmployeeSession

empSession.EmployeeID=someValue
End Sub

'----------------------------------


Any ideas or help appreciated

Steve
 
M

Marina

No, it will not.
In fact, I would make all the methods in EmployeeSession Shared. So you
don't need to bother instantiating it every time. Then something like this:

Public Shared Property EmployeeID() As Integer
Get
CInt(GetItem("EmployeeID"))
End Get
Set(Value As Integer)
SetItem("EmployeeID",Value)
End Set
End Property

Private Shared Function GetItem(itemName As String) As Object
Return HttpContext.Current.Session(itemName)
End Function

Private Shared Sub SetItem(itemName As String, itemValue As String)
HttpContext.Current.Session(itemName) = itemValue
End Sub

That way you can say EmployeeSession.EmployeeID, without instantiating
anything. Also, all the functionality of how everything is stored, is
actually in GetItem and SetItem, properties call out to that.

And lastly, please turn Option Strict On in your project, and make this the
default. This will save you many hours by catching errors at run time.
 
M

Marina

No Problem.
Second parameter to SetItem should be an Object, so you can put anything in
there. It was just to give the basic ide anyway...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top