This is taken from an old document, but it is still very relevant and works with pleasure ... I will post the link here, especially because it is an old link that may disappear. Taken from here.
Background
A Session object in ASP.Net can be used to store information specific to an individual site user. A session is indexed by a key name, and when used directly this way, it can lead to a large number of separate session names. An alternative approach is to instead create a singleton object to group related items and save this object with a given key name. "Singleton" is a general design pattern that indicates how to ensure that only one instance of a class exists at any time.
Benefits of Singleton Session Objects
- Grouping Session Elements for Organization Purposes
- Especially useful for a series of pages in transition, such as registering on a site. As soon as the process is completed, the object can be cleared from the session so that the memory can be restored (it is better to use server resources)
- Analyzing the effect of changes on session information is much simpler.
- Identify areas of the site that misuse the information (much clearer than just the name of the variable to determine if it is suitable for use)
- Intellisense for property names and types after accessing an object
Disadvantages of Singleton Session Objects
It is more difficult to see the number of individual elements in a session ASP.Net trace results do not display values ββinside an object Slow performance when using out-of-process session state (affects serialization)
Implementation
The first implementation step is to create a class file that represents a logical grouping of elements that should be stored together in one object. The following is an example class that demonstrates the technique:
public class singleton {
A page that wants to use this object simply does the following:
singleton oSingleton = singleton.GetCurrentSingleton(); oSingleton.FirstName = "Robert"; oSingleton.LastName = "Boedigheimer";
Typically, this method will store many other variables in a given class or will be used for a series of web pages that execute a process. Another advantage of using this for a process on a website is that all the memory needed for session variables can be cleared by simply deleting the link to a singleton object. A class can implement a method that clients can use to clear the link; it can be called Dispose () to follow a typical .Net pattern when the class provides a way to clear:
public static void Dispose() {
Conclusion
There are many advantages to using singleton objects stored in a Session object instead of using separate session keys to store information. It is useful for objects that should exist for the entire session (grouping of logical elements, impact analysis, intellisense, etc.) and especially for objects that are really needed only for a certain period of time on the website until the user completes a specific process (it is much easier to identify the misuse of variables and save resources when the process ends, but the session will continue).
Paul zahra
source share