2011-08-31 105 views
0

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

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个通用的私有方法来获取和设置Dictionary(Of T,T) GetFromSessionAsDictionary和SetInSessionAsDictionary。 (当没有任何东西返回像List(Of T)属性的空字典并在会话中设置时)

这应该允许我创建类型字典的许多公共属性(例如字典(字符串,字符串),字典字符串,整数))尽可能存储在会话中。有可能吗?如果是这样,怎么样?我已经去过了,但它让我感到困惑。

编辑

这是我已经结束了试图使其通用词典(T,T)之后做的,它的工作没有任何问题。我想要的是一种不会将类型限制为字典(字符串,整数)的解决方案。

私人常量_Dictionary1OfStringInt的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

嗯,我做到了我自己。我对传递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 

感谢您的建议!

+0

什么让你感到困惑?你在哪里遇到困难? – Oded

+0

@Hiral你可以展示你为Dictonary(Of T,T)所做的尝​​试吗?这将有助于 – msarchet

+1

也只是有趣的注意你可以做一些像'HttpContext.Current.Session(key)= If(value,New List(Of T)'而不是无处检查 – msarchet

回答

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