用屬性封裝 Session 及 ViewState 的存取
在 ASP.NET 程式中常會 Session 及 VIewState 儲存狀態,一般的寫法都是直接存取 Session 或 ViewState,例如將變數值儲存於 Session 的寫法如下。
- '將變數值儲存於 Session 中。
- Dim oValue As New NameValueCollection
- Session(KEY_SESSION) = oValue
- '由 Session 中轉型取得變數值。
- Dim oValue As NameValueCollection
- oValue = CType(Session(KEY_SESSION), NameValueCollection)
'將變數值儲存於 Session 中。
Dim oValue As New NameValueCollection
Session(KEY_SESSION) = oValue
'由 Session 中轉型取得變數值。
Dim oValue As NameValueCollection
oValue = CType(Session(KEY_SESSION), NameValueCollection)
不過上述的寫法有一些缺點:
1.每次存取 Session 時都要做型別轉換的動作,執行效能不佳。
2.容易因為 Session 鍵值錯誤,而造成不可預期的問題。
3.程式維護上較困難。例如改變鍵值或 Session 改儲存於 ViewState 中。
所以比較好的作法,就是使用屬性來封裝 Session 或 VIewState 的存取。以下的範例中,使用 SessionCollection 屬性來封裝 Session 的存取,ViewStateCollection 屬性來封裝 ViewState 的存取。
- Private KEY_SESSION = "_SeesionCollection"
- Private KEY_VIEWSTATE = "_ViewStateCollection"
- Private FSessionCollection As NameValueCollection
- Private FViewStateCollection As NameValueCollection
- ''' <summary>
- ''' 封裝 Session 存取的屬性。
- ''' </summary>
- Private ReadOnly Property SeesionCollection() As NameValueCollection
- Get
- '若區域變數為 Nothing 才重新取得,防止重覆做型別轉換的動作
- If FSessionCollection Is Nothing Then
- If Session(KEY_SESSION) Is Nothing Then
- FSessionCollection = New NameValueCollection()
- Session(KEY_SESSION) = FSessionCollection
- Else
- FSessionCollection = CType(Session(KEY_SESSION), NameValueCollection)
- End If
- End If
- Return FSessionCollection
- End Get
- End Property
- ''' <summary>
- ''' 封裝 ViewState 存取的屬性。
- ''' </summary>
- ''' <value></value>
- Private ReadOnly Property ViewStateCollection() As NameValueCollection
- Get
- '若區域變數為 Nothing 才重新取得,防止重覆做型別轉換的動作
- If FViewStateCollection Is Nothing Then
- If ViewState(KEY_VIEWSTATE) Is Nothing Then
- FViewStateCollection = New NameValueCollection()
- ViewState(KEY_VIEWSTATE) = FSessionCollection
- Else
- FViewStateCollection = CType(ViewState(KEY_VIEWSTATE), NameValueCollection)
- End If
- End If
- Return FViewStateCollection
- End Get
- End Property
Private KEY_SESSION = "_SeesionCollection"
Private KEY_VIEWSTATE = "_ViewStateCollection"
Private FSessionCollection As NameValueCollection
Private FViewStateCollection As NameValueCollection
''' <summary>
''' 封裝 Session 存取的屬性。
''' </summary>
Private ReadOnly Property SeesionCollection() As NameValueCollection
Get
'若區域變數為 Nothing 才重新取得,防止重覆做型別轉換的動作
If FSessionCollection Is Nothing Then
If Session(KEY_SESSION) Is Nothing Then
FSessionCollection = New NameValueCollection()
Session(KEY_SESSION) = FSessionCollection
Else
FSessionCollection = CType(Session(KEY_SESSION), NameValueCollection)
End If
End If
Return FSessionCollection
End Get
End Property
''' <summary>
''' 封裝 ViewState 存取的屬性。
''' </summary>
''' <value></value>
Private ReadOnly Property ViewStateCollection() As NameValueCollection
Get
'若區域變數為 Nothing 才重新取得,防止重覆做型別轉換的動作
If FViewStateCollection Is Nothing Then
If ViewState(KEY_VIEWSTATE) Is Nothing Then
FViewStateCollection = New NameValueCollection()
ViewState(KEY_VIEWSTATE) = FSessionCollection
Else
FViewStateCollection = CType(ViewState(KEY_VIEWSTATE), NameValueCollection)
End If
End If
Return FViewStateCollection
End Get
End Property
當要使用封裝 Session 及 ViewState 時,就如同存取屬性一樣。
- Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
- Dim iCount As Integer
- iCount = Me.SeesionCollection.Count
- Me.SeesionCollection.Add(iCount.ToString, iCount.ToString)
- iCount = Me.ViewStateCollection.Count
- Me.ViewStateCollection.Add(iCount.ToString, iCount.ToString)
- End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim iCount As Integer
iCount = Me.SeesionCollection.Count
Me.SeesionCollection.Add(iCount.ToString, iCount.ToString)
iCount = Me.ViewStateCollection.Count
Me.ViewStateCollection.Add(iCount.ToString, iCount.ToString)
End Sub
利用屬性封裝 Session 或 ViewState 的存取時,有下列優點:
1.撰寫程式碼時不用去理會 Seesion 或 ViewState,直接使用屬性即可,簡化程式碼及易讀性。
2.只做一次的型別轉換,執行效能較佳。
3.程式維護性佳。當 Session 或 ViewState 的鍵值變更或儲存目的改變時(如 Session 改為 ViewState),只需修改該屬性即可。
以上的做法雖然以 Session 及 ViewState 做示範,當然也可以使用相同方式來封裝 Application 及 Cache 的存取,也可達到上述的優點。