基于 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 关键字存储信息有着更多的优点。它 特别适合在一个站点内,一个时期内直到用户完成需要一个特别的过程的对象 ( 并且容易检验变量的错误,并且在过程完成时释放资源等 ) 。 **