会话.Net中的通用词典

时间:2011-08-31 14:25:30

标签: asp.net vb.net

我有一个名为SessionManager的泛型类来控制我在会话中存储的变量类型。我一直在为String,Integer,List(Of T)做这件事,没有任何问题。

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

我想对Dictionary(Of T,T)做同样的事情。我想要2个字典(Of T,T)的通用私有方法来获取和设置; GetFromSessionAsDictionary和SetInSessionAsDictionary。 (当没有任何内容返回空字典时,如List(Of T)属性并在会话中设置)

这应该允许我创建尽可能多的Type Dictionary公共属性(例如Dictionary(Of String,String),Dictionary(Of String,Integer))来存储在会话中。有可能吗?如果是这样,怎么样?我已经有了它,但它让我很困惑。

修改

这是我在尝试使它成为通用词典(Of T,T)后最终做的,并且它没有任何问题。我想要的是一个不会将类型约束为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

编辑2

嗯,我自己做了。我对传递TKey,TValue感到困惑,因为我认为T只能传递一次。

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

感谢您提供建议!

1 个答案:

答案 0 :(得分:0)

自己找到解决方案,抱歉浪费你的时间,谢谢!

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