Singleton object based on session
Session Based Singleton Object
Use Singleton Objects Stored Informion Object To Organize and Cleanup Session Based Information
background
In ASP.NET, the Session object is often used to store information about a site user.
It is a very effective way to personal users at a site. Session by using a keyword
As its index. When using this way, it can boot a large number of session names
We can also create a Singleton object to group according to a specific project.
To store the object and a given keyword. Singleton is a very ordinary design pattern, which
How to ensure that there is only one unique instance of a class in any time.
Singleton session object advantages
Grouping items in Session with organizations.
For a range of pages in a short period of time, it is especially useful, such as registering at a site. Once the process ends, the object can be cleared from the session, so the memory can be requested again.
(Better using server resources).
The influence analysis of modification of session information is easier.
The information in the user domain of a site is misused (if used, it is used to use only name only.
It is more clear).
Disadvantages of Singleton session object
It is difficult to view the number of personal information in the session ASP.NET tracking result cannot display values within the object.
When you use the process other than the process (affecting serialization), the performance will result in a decrease in performance.
achieve
Create a Singleton class:
VB.NET code:
Public Class Singleton
'Name That Will BE Used As Key for Session Object
Private const session_nsingleton 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 Construction So Cannot Create An Instance
'WITHOUT Using The Correct Method. This is
'this is crringal to prot all faumning
'as a singleton object, Objects of this
'Class Cannot Be create 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_gingleton) 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) = OSIingleton
Else
'Retrieve The Already Instance That Was Already Created
Osingleton = ctype (System.Web.httpContext.current.Session (session_gingleton), Singleton
END IF
'Return The Single Instance of this Class That Was Stored In The Session
Return Osingleton
END FUNCTION
END CLASS
C # code:
Public Class Singleton
{
// Name That Will Be Used As Key for Session Object
Private const string session_nsingleton = "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 Construction So Cannot Create An Instance
// WitHout Using The Correct Method. This is
// this is crringal to protly importing
// as a singleton object, Objects of this
// Class Cannot Be create from outside this
// Class
Private singleton ()
{
}
// CREATE AS A Static Method So this can be called us // 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_gingleton])
{
// 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_nsingleton] = OSIingleton;
}
Else
{
// Retrieve the already instance That Was Already Created
Osingleton = (Singleton) System.Web.httpContext.current.Session [session_gingleton];
}
// Return The Single Instance of this Class That Was Stored In The Session
Return Osingleton;
}
}
Use this object in the page:
Singleton Osingleton = singleton.getcurrentsingleton ();
Osingleton.firstname = "robert";
Osingleton.lastname = "boEdigheimer";
This technology can store more variables in a given class, or by a series of web pages
Use, execute a certain process. Another advanced method is used in the process of a Web site, through
Singleton object references, the session variable requested in all memory can be easily cleared. Category
Implement a client to clear the reference method. Can be named Dispose, similar to the clear in .NET mode
Except for the object.
Public static void dispose ()
{
// Cleanup this Object So That GC CAN Reclaim Space
System.Web.httpContext.current.Session.Remove (session_gingleton);
}
in conclusion
In the session object, use Singleton object storage information has more advantages than using the session keyword storage information. It is especially suitable for one site, and within a period of time until the user completes an object that requires a special process (and easy to verify the variable error, and release resources when the process is completed).