王朝网络
分享
 
 
 

基于Session的单件(Singleton)对象

王朝other·作者佚名  2006-01-09
宽屏版  字体: |||超大  

基于Session的单件(Singleton)对象

Session Based Singleton Object

Use singleton objects stored in the Session object to organize and cleanup session based information

背景

在ASP.Net 当中,Session 对象经常被用来存储一个站点用户的信息,这对

于一个站点的个人用户来说是很有效的方法。Session通过使用一个关键字

作为它的索引。当应用这种方式使用它时,它可以引导大量的session名

称. 我们也可以建立一个singleton 对象来根据具体的项目进行分组,用

来存储给对象和给定的关键字。singleton 是一个很普通的设计模式,它规

定了如何保证在任意时间内只存在一个类的唯一的实例。

Singleton Session对象的优点

以组织为目的对session 中的项进行分组。

对在短时间过程中的一系列页面特别有用,如在一个站点注册。一旦

过程结束,对象可以从session 中清除,所以内存可以再次被请求

(使用服务器资源更佳)。

修改session信息的影响力分析更容易。

一个站点的用户域的信息被误用(如使用得当,比起只使用名称来确

认更加清晰)。

Singleton Session对象的缺点

很难查看session 中的个人信息的数量

ASP.Net 的跟踪结果不能显示对象内的值

当使用进程以外的方式存储session(影响串行化)时,会导致性能下降。

实现

建立一个singleton类:

VB.net代码:

Public Class singleton

'Name that will be used as key for Session object

Private Const SESSION_SINGLETON As String = "SINGLETON"

'Variables to store the data (used to be individual

'session key/value pairs)

Dim _lastName As String = ""

Dim _firstName As String = ""

Public Property LastName() As String

Get

Return _lastName

End Get

Set(ByVal Value As String)

_lastName = Value

End Set

End Property

Public Property FirstName() As String

Get

Return _firstName

End Get

Set(ByVal Value As String)

_firstName = Value

End Set

End Property

'Private constructor so cannot create an instance

' without using the correct method. This is

' this is critical to properly implementing

' as a singleton object, objects of this

' class cannot be created from outside this

' class

Private Sub New()

End Sub

'Create as a static method so this can be called using

'just the class name (no object instance is required).

'It simplifies other code because it will always return

'the single instance of this class, either newly created

'or from the session

Public Shared Function GetCurrentSingleton() As singleton

Dim oSingleton As singleton

If System.Web.HttpContext.Current.Session(SESSION_SINGLETON) Is Nothing Then

'No current session object exists, use private constructor to

'create an instance, place it into the session

oSingleton = New singleton

System.Web.HttpContext.Current.Session(SESSION_SINGLETON) = oSingleton

Else

'Retrieve the already instance that was already created

oSingleton = CType(System.Web.HttpContext.Current.Session(SESSION_SINGLETON), singleton)

End If

'Return the single instance of this class that was stored in the session

Return oSingleton

End Function

End Class

C#代码:

public class singleton

{

//Name that will be used as key for Session object

private const string SESSION_SINGLETON = "SINGLETON";

//Variables to store the data (used to be individual

// session key/value pairs)

string lastName = "";

string firstName = "";

public string LastName

{

get

{

return lastName;

}

set

{

lastName = value;

}

}

public string FirstName

{

get

{

return firstName;

}

set

{

firstName = value;

}

}

//Private constructor so cannot create an instance

// without using the correct method. This is

// this is critical to properly implementing

// as a singleton object, objects of this

// class cannot be created from outside this

// class

private singleton()

{

}

//Create as a static method so this can be called using

// just the class name (no object instance is required).

// It simplifies other code because it will always return

// the single instance of this class, either newly created

// or from the session

public static singleton GetCurrentSingleton()

{

singleton oSingleton;

if (null == System.Web.HttpContext.Current.Session[SESSION_SINGLETON])

{

//No current session object exists, use private constructor to

// create an instance, place it into the session

oSingleton = new singleton();

System.Web.HttpContext.Current.Session[SESSION_SINGLETON] = oSingleton;

}

else

{

//Retrieve the already instance that was already created

oSingleton = (singleton)System.Web.HttpContext.Current.Session[SESSION_SINGLETON];

}

//Return the single instance of this class that was stored in the session

return oSingleton;

}

}

在页面中使用这个对象:

singleton oSingleton = singleton.GetCurrentSingleton();

oSingleton.FirstName = "Robert";

oSingleton.LastName = "Boedigheimer";

这项技术可以在给定的类中存储更多的变量,也可以被一系列的Web页面来使

用,执行一定的过程。另外一个高级使用方法是在一个Web站点的过程中使用,通过对

singleton对象引用的移除,所有的内存中请求的session变量可以很容易的清除。 类可以

实现一个客户端用来清除引用的方法。可以命名为Dispose,类似在.NET 模式中提供的清

除对象的方法。

public static void Dispose()

{

//Cleanup this object so that GC can reclaim space

System.Web.HttpContext.Current.Session.Remove(SESSION_SINGLETON);

}

结论 在Session对象中,使用singleton对象存储信息比使用session关键字存储信息有着更多的优点。它特别适合在一个站点内,一个时期内直到用户完成需要一个特别的过程的对象(并且容易检验变量的错误,并且在过程完成时释放资源等)。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有