うーん、アイデアはありませんか?!
とにかく、他に選択肢がないので、私は自分のコードを使用してカプセル化を行うことにしました。公共の利益のために、ここに自分のコードを投稿することで、興味のある兄弟に利益がもたらされる可能性があります。
ここでの主なアイデアは、大きなWebアプリケーションを開発し、ユーザーの選択をマルチレベルスコープで保存する必要があることです。たとえば、設定による順序の保存、ページあたりのアイテム数など、およびマルチレベルスコープでの設定の長期保存などです。期間(Cookieの場合)または短期間の(セッションの場合)。
また、これらの選択が多すぎるため、一部のブラウザで許可されるCookieの数が少ないため、複数のCookieを使用することを余儀なくされました。
このトリックを実行するためのカプセル化された関数は次のとおりです。
Public Enum StoredPropertyScope As SByte
SessionAndCookies = 0
SessionOnly = 1
CookiesOnly = 2
End Enum
Public Shared Function GetStoredProperty(ByVal group As String, ByVal Key As String, Optional scope As StoredPropertyScope = StoredPropertyScope.SessionAndCookies) As Object
If (scope = StoredPropertyScope.SessionAndCookies Or scope = StoredPropertyScope.SessionOnly) And Not (HttpContext.Current.Session(group & "-" & Key) Is Nothing) Then
Return HttpContext.Current.Session(group & "-" & Key)
Else
If (scope = StoredPropertyScope.SessionAndCookies Or scope = StoredPropertyScope.CookiesOnly) And Not (Request.Cookies(group) Is Nothing) Then
Return Request.Cookies(group)(Key)
Else
Return Nothing
End If
End If
End Function
Public Shared Sub SetStoredProperty(ByVal group As String, ByVal key As String, value As String, Optional scope As StoredPropertyScope = StoredPropertyScope.SessionAndCookies)
If scope = StoredPropertyScope.SessionAndCookies Or scope = StoredPropertyScope.CookiesOnly Then
If Request.Cookies(group) Is Nothing Then
Response.Cookies(group)(key) = value
Response.Cookies(group).Expires = Now.AddYears(1)
Else
Dim OldValues As NameValueCollection = Request.Cookies.Get(group).Values
OldValues.Item(key) = value
Response.Cookies.Get(group).Values.Clear()
Response.Cookies.Get(group).Values.Add(OldValues)
Response.Cookies(group).Expires = Now.AddYears(1)
End If
End If
If scope = StoredPropertyScope.SessionAndCookies Or scope = StoredPropertyScope.SessionOnly Then
HttpContext.Current.Session(group & "-" & key) = value
End If
End Sub
Public Overloads Shared Sub RemoveStoredProperty(ByVal group As String, ByVal key As String, Optional scope As StoredPropertyScope = StoredPropertyScope.SessionAndCookies)
If scope = StoredPropertyScope.SessionAndCookies Or scope = StoredPropertyScope.CookiesOnly Then
If Not (Request.Cookies(group) Is Nothing) AndAlso Request.Cookies(group).HasKeys Then
Dim OldValues As NameValueCollection = Request.Cookies.Get(group).Values
OldValues.Remove(key)
Response.Cookies.Get(group).Values.Clear()
Response.Cookies.Get(group).Values.Add(OldValues)
Response.Cookies(group).Expires = Now.AddYears(1)
End If
End If
If scope = StoredPropertyScope.SessionAndCookies Or scope = StoredPropertyScope.SessionOnly Then
HttpContext.Current.Session.Remove(group & "-" & key)
End If
End Sub
Public Overloads Shared Sub RemoveStoredProperty(ByVal group As String)
'remove all from cookies only
Response.Cookies(group).Expires = DateTime.Now.AddDays(-1)
End Sub
Public Shared Function CheckStoredProperty(ByVal group As String, ByVal key As String) As StoredPropertyScope
'return where is property stored or -1 if not exists
Dim result As StoredPropertyScope = -1
If (Not Request.Cookies(group) Is Nothing) AndAlso (Not Request.Cookies(group)(key) Is Nothing) Then
result = StoredPropertyScope.CookiesOnly
End If
If Not HttpContext.Current.Session(group & "-" & key) Is Nothing Then
If result = -1 Then result = StoredPropertyScope.SessionOnly Else result = StoredPropertyScope.SessionAndCookies
End If
Return result
End Function
使用方法1-最初に、プロパティグループとサブキーのすべての名前をデキュメンテーションする必要があります。これは、文字列値として使用するため、正しいことを確認する責任があります。
2-値を保存するには、次のようなコードを使用します。
SetStoredProperty("userSelections", "itemsOrderedBy", "Price", StoredPropertyScope.SessionAndCookies)
- 最初のパラメーターはグループ名であり、これはCookie名になり、2番目のパラメーターはproberty名(cookieのサブ値)であり、2番目のパラメーターはproblertyの値であり、4番目のパラメーターはオプションで場所を決定します。この値を保存します(セッションのみまたはCookieのみ、あるいはその両方)。
*注(Cookieの有効期限は1年で、コードから直接変更できます)
3-値を取得するには:
GetStoredProperty("userSelections", "itemsOrderdBy", StoredPropertyScope.CookiesOnly)
- 3番目のパラメーターはオプションであり、SessionAndCookiesを選択した場合、戻り値の優先順位はセッションになります
4-値を削除するには:
RemoveStoredProperty("userSelections")
RemoveStoredProperty("userSelections", "itemsOrderdBy", StoredPropertyScope.SessionAndCookies)
- 最初の行はグループ全体(Cookie)を削除し、Cookieにのみ使用します。2行目は、オプションの3番目のパラメーターで決定されたスコープから指定されたプロパティを削除します
5-プロパティが保存されている場所:
CheckStoredProperty("userSelections", "itemsOrderdBy")
- この関数はprobertyを検索し、存在する場合はどこにあるか(StoredPropertyScope enum)を返し、存在しない場合は-1を返します。
それがお役に立てば幸いです。もっと良い解決策があれば教えてください。ありがとう