开发者

Generic Dictionary in Session .Net

I have a generic class called SessionManager to control the types of variables I store in session. I have been doing it for String, Integer, List(Of T) without any issues like this.

Public NotInheritable Class SessionManager

    '''<remarks>
    '''Private constructor to prevent instantiation of, or inheritance from, this class.
    '''</remarks>
    Private Sub New()
    End Sub


       Private Const _PropertyOfSomeClass As String = "PROPERY_OF_SOME_CLASS"
       Private Const _ListOfSomeClass As String = "LIST_OF_SOME_CLASS"

       Public Shared Property PropertyOfSomeClass() As SomeClass
            Get
                Return GetFromSession(Of SomeClass)(_PropertyOfSomeClass)
            End Get
            Set(value As SomeClass)
                SetInSession(Of SomeClass)(_PropertyOfSomeClass, value)
            End Set
        End Property


        Public Shared Property ListOfSomeClass() As List(Of SomeClass)
            Get
                Return GetFromSessionAsList(Of SomeClass)(_ListOfSomeClass)
            End Get
            Set(value As List(Of SomeClass))
                SetInSessionAsList(Of SomeClass)(_ListOfSomeClass, value)
            End Set
        End Property


        Private Shared Function GetFromSession(Of T)(key As String) As T
            Dim obj As Object = HttpContext.Current.Session(key)
            If obj Is Nothing Then
                Return Nothing
            End If
            Return DirectCast(obj, T)
        End Function

        Private Shared Function GetFromSessionAsList(Of T)(key As String) As List(Of T)
            Dim obj As Object = HttpContext.Current.Session(key)
            If obj Is Nothing Then
                SetInSessionAsList(Of T)(key, New List(Of T))
                Return New List(Of T)
            End If
            Return DirectCast(obj, List(Of T))
        End Function

        Private Shared Sub SetInSession(Of T)(key As String, value As T)
            If value Is Nothing Then
                HttpContext.Current.Session.Remove(key)
            Else
                HttpContext.Current.Session(key) = value
            End If
        End Sub

        Private Shared Sub SetInSessionAsList(Of T)(key As String, value As List(Of T))
            If value Is Nothing Then
                HttpContext.Current.Session(key) = New List(Of T)
            Else
                HttpContext.Current.Session(key) = value
            End If
        End Sub

End Class

I would like to do the same for Dictionary(Of T, T). I would like 2 generic private methods for Dictionary(Of T, T) for getting and setting; GetFromSessionAsDictionary and SetInSessionAsDictionary. (When nothing returns empty dictionary like the List(Of T) properties and sets in session)

And this should allow me to 开发者_运维百科create as many public properties of Type Dictionary (e.g. Dictionary(Of String, String), Dictionary(Of String, Integer)) as possible to store in session. Is it possible to do? If so, how? I have already had a go at it but it's confused the hell out of me.

EDIT

This is what I have ended up doing after trying to make it generic Dictionary(Of T, T), and it works without any issues. What I want instead is a solution that would not constraint the type to Dictionary(Of String, Integer).

Private Const _Dictionary1OfStringInt As String = "DICTIONARY_1_OF_STRING_INT"

Public Shared Property DictionaryOf As Dictionary(Of String, Integer)
    Get
        Return GetFromSessionAsDictionary(_Dictionary1OfStringInt)
    End Get
    Set(value As Dictionary(Of String, Integer))
        SetInSessionAsDictionary(_Dictionary1OfStringInt, value)
    End Set
End Property

Private Shared Function GetFromSessionAsDictionary(key As String) As Dictionary(Of String, Integer)
    Dim obj As Object = HttpContext.Current.Session(key)
    If obj Is Nothing Then
        SetInSessionAsDictionary(key, New Dictionary(Of String, Integer))
        Return New Dictionary(Of String, Integer)
    End If
    Return DirectCast(obj, Dictionary(Of String, Integer))
End Function

Private Shared Sub SetInSessionAsDictionary(key As String, value As Dictionary(Of String, Integer))
    If value Is Nothing Then
        HttpContext.Current.Session(key) = New Dictionary(Of String, Integer)
    Else
        HttpContext.Current.Session(key) = value
    End If
End Sub

EDIT 2

Well, I did it myself. I was confused about passing TKey, TValue as I thought T could only be passed once.

Private Shared Function GetFromSessionAsDictionary(Of TKey, TValue)(key As String) As Dictionary(Of TKey, TValue)
    Dim obj As Object = HttpContext.Current.Session(key)
    If obj Is Nothing Then
        SetInSessionAsDictionary(key, New Dictionary(Of TKey, TValue))
        Return New Dictionary(Of TKey, TValue)
    End If
    Return DirectCast(obj, Dictionary(Of TKey, TValue))
End Function

Private Shared Sub SetInSessionAsDictionary(Of TKey, TValue)(key As String, value As Dictionary(Of TKey, TValue))
    HttpContext.Current.Session(key) = If(value, New Dictionary(Of TKey, TValue)())
End Sub

Thanks for advising!


Found the solution myself, sorry for wasting your time, Thanks!

Private Shared Function GetFromSessionAsDictionary(Of TKey, TValue)(key As String) As Dictionary(Of TKey, TValue)
    Dim obj As Object = HttpContext.Current.Session(key)
    If obj Is Nothing Then
        SetInSessionAsDictionary(key, New Dictionary(Of TKey, TValue))
        Return New Dictionary(Of TKey, TValue)
    End If
    Return DirectCast(obj, Dictionary(Of TKey, TValue))
End Function

Private Shared Sub SetInSessionAsDictionary(Of TKey, TValue)(key As String, value As Dictionary(Of TKey, TValue))
    HttpContext.Current.Session(key) = If(value, New Dictionary(Of TKey, TValue)())
End Sub
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜