A Web Session Singleton

by Moridin8 18. March 2008 20:14

You could consider this singleton to be the web application friendly version of the ThreadSingleton (Well... almost).  It lives inside the HttpContext Session dictionary as a standard session serialized object instead of staying in state against a specific thread and ss usual for session data it is (de)serialized against which ever session serialisation source you are using

In more complicated incarnations this form of class can allow all sorts of extensions to your web session model.  Using the various facilities available in .NET for instance you can engineer and associate all forms of models directly against the session data in a much more OOP friendly manner.

This is my prefered way of working with session stated data.  I sometimes wonder why so many other people don't themselves...

[Serializable]
public sealed class SessionSingleton
{
    private const string SESSION_KEY 
                        
= "MySession_3BC433AB-3925-4bee-9EDD-7F874D241AD0"; /// <summary> /// Some basic instance data. /// </summary> private string _SomeInstanceData; /// <summary> /// Gets or sets some instance data. /// </summary> /// <value>Some instance data.</value> public string SomeInstanceData { get { return this._SomeInstanceData; } set { this._SomeInstanceData = value; } } /// <summary> /// Initializes a new instance of the <see cref="SessionSingleton"/> class. /// This is privately invoked /// </summary> private SessionSingleton() { this._SomeInstanceData = "Hi!"; } /// <summary> /// Gets the singleton. /// </summary> /// <value>The singleton.</value> public static SessionSingleton Session { get { // double check locking... if( HttpContext.Current.Session[SESSION_KEY] == null ) lock( HttpContext.Current.Session.SyncRoot ) if( HttpContext.Current.Session[SESSION_KEY] == null ) HttpContext.Current.Session[SESSION_KEY] =
                                                            
new SessionSingleton(); return HttpContext.Current.Session[SESSION_KEY] as SessionSingleton; } } }

 

Tags: , , ,

Powered by BlogEngine.NET 1.5.0.7

About Matt R.Warren

MeMy name is Matt and I am the current tenant of this small corner of the internet. I mostly architect, design and prototype applications that use .NET with C# and a little C++/CLI for Enterprise although I am aware of and enjoy fully embracing Java based solutions and alternatives such as Mono/Linux.  

I have worked on projects ranging from small tools to large distributed real-time Enterprise systems ranging from EPOS and real-time/JIT stock management systems, to distributed applications for National/International Utility, Healthcare, Insurance and Finance  in the private sector in both the USA and the EU.

My LinkedIn Profile (Opens new window/tab)

“Matt is one of the brightest people I've worked with. His in-depth knowledge of the .NET frameworks has been a tremendous benefit to nVISIA and our clients. His knowledge of software architecture in general allows him to architect systems for the best fit to his client's needs.” 
Dan Christopherson , Technical Director , nVISIA

“I had the distinct pleasure of working with Matt at nVisia. Matt's understanding of the Microsoft Technical space is outstanding. He is constantly working on improving his technical skills and rapidly masters any new technology that he encounters. He is an excellent teacher and a wonderful asset for any size team.” 
Jim Harnden , Senior Technical Architect , nVISIA

“Matt Warren is a very talented developer with great capacity for self study, investigation and adapts to new languages and frameworks with ease. He has an excellent grasp of software architecture and modern development principles. He has proven himself time and time again to be a hard worker and someone who can get the job done when you're in a tight spot.” 
Andrew Jump , Partner, C# Developer , Contegra

This website represents some of my spare time.  My small presence on the web between my family and my career.  I hope over time you find many useful things here.