在ASP.Net 当中,Session 对象经常被用来存储一个站点用户的信息,这对
于一个站点的个人用户来说是很有效的方法。Session通过使用一个关键字
作为它的索引。当应用这种方式使用它时,它可以引导大量的session名
称. 我们也可以建立一个singleton 对象来根据具体的项目进行分组,用
来存储给对象和给定的关键字。singleton 是一个很普通的设计模式,它规
定了如何保证在任意时间内只存在一个类的唯一的实例。
Singleton 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); } |
本文地址:http://com.8s8s.com/it/it44730.htm