假如这个世界上只剩下你一个人,当你正坐在屋子里的时候,这时突然响起了敲门声...

用属性封装 Session 及 VIewState 的存取

上一篇 / 下一篇  2008-05-05 20:51:41

查看( 25 ) / 评论( 4 )
在 ASP.NET 程序中常会 Session 及 VIewState 储存状态,一般的写法都是直接存取 Session 或 ViewState,例如将变量值储存于 Session 的写法如下。ITPUB个人空间Z2u&_ w9g~S?b
1        '将变量值储存于 Session 中。ITPUB个人空间PEEW t
2        Dim oValue As New NameValueCollectionITPUB个人空间C`%F`&Cf7F/@
3        Session(KEY_SESSION) = oValueITPUB个人空间,e,][vz }L'{i
4ITPUB个人空间FJJ7vKk%M7q
5        '由 Session 中转型取得变量值。
K IXu8e6B0 6        Dim oValue As NameValueCollectionITPUB个人空间6KYE7F1k
7        oValue = CType(Session(KEY_SESSION), NameValueCollection)
!s Gl[x/N*|0 8ITPUB个人空间cr O(Ny[T
ITPUB个人空间$J]y(iGK| ?)N
ITPUB个人空间v3Rs&u'C%|^q
不过上述的写法有一些缺点:
GUx+C1{-g-g'D0
&_^5~)M'G5g/Q6X*k0 1.每次存取 Session 时都要做型别转换的动作,执行效能不佳。ITPUB个人空间`A|]j SWA)?
ITPUB个人空间9O"T lA3A"to+ye-~$x
2.容易因为 Session 键值错误,而造成不可预期的问题。ITPUB个人空间R6Ee&J4@K
ITPUB个人空间Fq9Z;]4qj1{
3.程序维护上较困难。例如改变键值或 Session 改储存于 ViewState 中。
/Rc9ME"u7[0 ITPUB个人空间-km eyaM%}9dK

2Xy3BJbD ]+L0 ITPUB个人空间 U}z4v:G \
所以比较好的作法,就是使用属性来封装 Session 或 VIewState 的存取。以下的范例中,使用 SessionCollection 属性来封装 Session 的存取,ViewStateCollection 属性来封装 ViewState 的存取。ITPUB个人空间m'B`o^4R(|C
1    Private KEY_SESSION = "_SeesionCollection"ITPUB个人空间J/p5{6n-VBu A
2    Private KEY_VIEWSTATE = "_ViewStateCollection"
9E4l8o1K6G3G+}0 3    Private FSessionCollection As NameValueCollectionITPUB个人空间9Z B9Wu|/b^d
4    Private FViewStateCollection As NameValueCollectionITPUB个人空间6bS2y/uM"[;Cz
5ITPUB个人空间6a QLK6V3Q
6    /**/''' <summary>
xY)hP ~G(A3W0PX7{0 7    ''' 封装 Session 存取的属性。ITPUB个人空间 b7hSW%uM(Ea
8    ''' </summary>ITPUB个人空间 m.|&nfN0J d
9    Private ReadOnly Property SeesionCollection()Property SeesionCollection() As NameValueCollectionITPUB个人空间+ZHTc G5?
10        Get
D1SOa,O9P{L0 11            '若区域变量为 Nothing 才重新取得,防止重复做型别转换的动作ITPUB个人空间4N3m x8K/K*w9YM;{P1?
12            If FSessionCollection Is Nothing Then
,M5y5x(T%x,U&V0 13                If Session(KEY_SESSION) Is Nothing Then
0g\%j JO6a_.f3\)uS6l0 14                    FSessionCollection = New NameValueCollection()
US[;S1e/j X0 15                    Session(KEY_SESSION) = FSessionCollection
!tQ,U)~+]!A1in0 16                Else
+ug(Nl|+M"U0 17                    FSessionCollection = CType(Session(KEY_SESSION), NameValueCollection)ITPUB个人空间 o Uq.Uv
18                End If
,E+i2Cf6@r y0 19            End If
K0l&[}t9A0 20            Return FSessionCollectionITPUB个人空间bvLwQyo"fR
21        End GetITPUB个人空间 Y'@W V,hI ^
22    End PropertyITPUB个人空间9]5jBU5O ~.[
23
8hz:q&@/x0 24    /**/''' <summary>
-AAi"w#thZ0 25    ''' 封装 ViewState 存取的属性。ITPUB个人空间"_)Gx"?'UK"DR&c
26    ''' </summary>
r6\2N3DGZmTEN0 27    ''' <value></value>ITPUB个人空间 Gb9KO;xK D5R
28    Private ReadOnly Property ViewStateCollection()Property ViewStateCollection() As NameValueCollection
t$bj4ph e1J0 29        Get
:c#a1j5|nUi0 30            '若区域变量为 Nothing 才重新取得,防止重复做型别转换的动作ITPUB个人空间a%Vz5F U `
31            If FViewStateCollection Is Nothing Then
tH#aF6R*\n9N N0 32                If ViewState(KEY_VIEWSTATE) Is Nothing ThenITPUB个人空间:x*t4r)FR m#L
33                    FViewStateCollection = New NameValueCollection()
Mo SQ1r A7V0 34                    ViewState(KEY_VIEWSTATE) = FSessionCollection
Y!m#r4jr%t.[e j0 35                ElseITPUB个人空间0XEa7[4\2o
36                    FViewStateCollection = CType(ViewState(KEY_VIEWSTATE), NameValueCollection)ITPUB个人空间2g~5Im.P6o&F2D
37                End IfITPUB个人空间Y!R+i;bAEkJI"}
38            End IfITPUB个人空间B O?_T)qe
39            Return FViewStateCollectionITPUB个人空间6z3t#P;NF+G.I
40        End GetITPUB个人空间dc ~6P|Oz0~c
41    End PropertyITPUB个人空间%Tg\ m:H3D r1_R
42
(Jz)W8U6A#u+c)Hq o0
KS3Z G%[5Fp I0
t;R/ZWV8\0 当要使用封装 Session 及 ViewState 时,就如同存取属性一样。
L'~ r tQ3c0 1    Protected Sub Button1_Click()Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.ClickITPUB个人空间|(a9DLZek7D%z
2        Dim iCount As IntegerITPUB个人空间Z'CLj8j%A aW
3ITPUB个人空间a{e4\*w"X&r
4        iCount = Me.SeesionCollection.CountITPUB个人空间4C@#a;ysV~
5        Me.SeesionCollection.Add(iCount.ToString, iCount.ToString)
6^3fc7ak`3I#a0 6ITPUB个人空间 k{(N9V4cdB['I
7        iCount = Me.ViewStateCollection.CountITPUB个人空间f@2L#zr#p6I
8        Me.ViewStateCollection.Add(iCount.ToString, iCount.ToString)
%WR P ^nE[4o0 9    End Sub
nt1e`+c V%I0 ITPUB个人空间??']m`"_D&V
ITPUB个人空间;R;p5k?E;~@Q
利用属性封装 Session 或 ViewState 的存取时,有下列优点:
$Sp_ A7j&}q(wo+G0
Dw\;H kFOel0 1.撰写程序代码时不用去理会 Seesion 或 ViewState,直接使用属性即可,简化程序代码及易读性。ITPUB个人空间Fz:Cw*HWa

3f`&a7k"yy#o!n|0 2.只做一次的型别转换,执行效能较佳。
R\H*iZt0
/r_CPA g0 3.程序维护性佳。当 Session 或 ViewState 的键值变更或储存目的改变时(如 Session 改为 ViewState),只需修改该属性即可。
`2d\5s:sS0
O3g%g:M)m8K0 ITPUB个人空间#S@6F D&aPD;@
ITPUB个人空间-W nS9G+a/kv
以上的做法虽然以 Session 及 ViewState 做示范,当然也可以使用相同方式来封装 Application 及 Cache 的存取,也可达到上述的优点。

TAG:

zengshaojie的个人空间 zengshaojie 发布于2008-05-05 20:54:56
学习ing
Icy.Fang 的个人空间 mosticy 发布于2008-05-06 13:41:54
挺好的。支持。
在路上...吻别;留什么给你?伤痕! ziyi0808 发布于2008-05-06 16:26:35
换一种思维就会得到以外的惊喜
记录:Rocky月影 rockwl2001 发布于2008-05-07 03:39:04
恩,不错,封装一下,清楚很多。
我来说两句

(可选)

日历

« 2008-05-12  
    123
45678910
11121314151617
18192021222324
25262728293031

数据统计

  • 访问量: 3485
  • 日志数: 82
  • 建立时间: 2008-02-08
  • 更新时间: 2008-04-25

RSS订阅

Open Toolbar