Strongly typed session object

In .net, whether you know it or not, just about everything is a datasource. Or maybe you should consider everything stateful as a data source.

The problem is that where these things are stateful, may are non-strongly typed. IE: data is passed as object.

Why would you do that to the people who have to support your code?

Here is an example:
dim x as string = session(“SomeKey”).tostring
dim y as integer = convertibility(Session(“SomeKey”))

As you can see, some sort of casting is needed, but if I do this by hand, I will end up doing this everywhere by hand. A lot of code.

A simple solution is to write your own session wrapper.

Public Class MySession
Private Shared Session As Web.SessionState.HttpSessionState = System.Web.HttpContext.Current.Session
Public Overloads Shared Function ToString(ByVal key As String) As String
Return Session(key).ToString()
End Function
Public Shared Function ToInteger(ByVal key As String) As Integer
Return Convert.ToInt32(Session(key))
End Function
End Class
Now when you use this class you get:
dim x as string = MySession.ToString(“SomeKey”)
dim y as integer = MySession.ToInteger(“SomeKey”)

But..But..But..That’s more code!!!

What if the key is missing? Then you have to handle missing key code how many times on your own? Lets wrap that into this class too.

Public Class MySession
Private Shared Session As Web.SessionState.HttpSessionState = System.Web.HttpContext.Current.Session
Public Overloads Shared Function ToString(ByVal key As String) As String
Dim o As Object
Dim ret As String
Try
o = Session(key)
Catch ex As Exception
Throw New MissingKeyException
End Try
Try
ret = Convert.ToString(o)
Catch ex As Exception
Throw New FailedCastException
End Try
Try
Return ret
Catch ex As Exception
Throw New FailedSessionRetreival
End Try
End Function
Public Shared Function ToInteger(ByVal key As String) As Integer
Dim o As Object
Dim ret As Integer
Try
o = Session(key)
Catch ex As Exception
Throw New MissingKeyException
End Try
Try
ret = Convert.ToInt32(o)
Catch ex As Exception
Throw New FailedCastException
End Try
Try
Return ret
Catch ex As Exception
Throw New FailedSessionRetreival
End Try
End Function

Class MissingKeyException
Inherits FailedSessionRetreival
Public Sub New()
MyBase.New()
End Sub
End Class

Class FailedCastException
Inherits FailedSessionRetreival
Public Sub New()
MyBase.New()
End Sub
End Class

Class FailedSessionRetreival
Inherits Exception
Public Sub New()
MyBase.New()
End Sub
End Class
End Class

Now with this, we can cast and trap at the same time. And If we need to handle missing keys differently, then Extend the exceptions even further.

Lets say you don’t want to handle missing data with exceptions. No problem, write a method to check the keys collection and see if the you are looking for is there, if so, the retrieve value, if not then manage the missing information by your business rules.

I would have a public shared function called:

Public Shared DoesKeyExist(Key as String) as boolean

Don’t limit yourself. There is a whole lot more you can do with this class than what I have shown here.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.