CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Mark DiGiovanni

Software Development and Supporting Technologies

Managing session state in the Singleton Model

Effective management of user state becomes difficult as the application grows during development. Available memory on the server decreases each time the developer decides to persist data in session object. This can have a negative impact on the application’s performance, as well as other applications that exist on the server.

Available memory is one consideration; however I will be focusing on the benefits of better organization of session objects. If you have not read the following article, do so now http://www.aspalliance.com/robertb/articles.aspx?articleId=4.

Generally speaking, each page on a web application may have a dataset or multiple datasets providing the data the user will be interacting with. Some of this data may be data that is general to the entire site; some of it may be specific to the page. View state may be one option for page specific data, but that’s a tangent for another time. What if you have a series of forms that take the user form point A to point E while collecting data from points B, C, and D in-between, such as a wizard assisting a user in filling out an employment application? What about a shopping cart that is available for the user while they are browsing different areas of the site? You may have created several different datasets, one for each page, maybe a few other objects to handle one-off bits of information, whatever the case, you may have ended up with ten, twenty, or thirty or so objects in session. What if you needed to clear out or test specific objects being stored in session and there are a dozen or so objects persisted? With the Singleton model, everything can be typed allowing you to access those objects using intelisense.

I recently deployed a management application that handles the admin tasks for all .NET deployed web apps for a certain client. This application handles users, roles, role memberships, as well as various application specific areas. When the user logs in to the management system, they must select a server, which provides a list of applications. The user then chooses an application to administer. This information is stored in one singleton object, and is made available to every page.

Below is a portion of my session object:

using System;

namespace CSharpWeb
{
///


/// Manages Session Objects
///

public class SessionManager
{
//Make the constructor private
private SessionManager()
{}


//Name of the session variable
private const string SESSION_MANAGER = "SESSION_MANAGER";
string _userName = "";
string _firstName = "";
string _lastName = "";
string _selectedAppConString = "";
string _server = "";


//For brevity, only the below public property is displayed...
//you get the idea, one public property for every private member.
public string UserName
{
get
{
return _userName;
}
set
{
_userName = value;
}
}


///


/// Gets the SessionManager object out of session and returns it to the caller.
///

/// Session Manager typed object
public static SessionManager GetSessionManager()
{
SessionManager sMngr;
if(System.Web.HttpContext.Current.Session[SESSION_MANAGER] == null)
{
sMngr = new SessionManager();
System.Web.HttpContext.Current.Session[SESSION_MANAGER] = sMngr;
}
else
sMngr = (SessionManager)System.Web.HttpContext.Current.Session[SESSION_MANAGER];

return sMngr;
}
}


///


/// Calling class, this could be a class, but normally would be the WebForm itself.
///

public class CallingClass
{
public CallingClass()
{}
///
/// Call here to retrieve relevant data from the data source.
///

public void FillData()
{
//Get the data out of session
SessionManager sMgr = SessionManager.GetSessionManager();
//Make the calls here to retrive the relevant data:
sMgr.UserName = "Retrived user's Name Here";
//Set other properties of the Session Manager.

/*
No need to put the Session Manager "Back Into Session." This is handled
because the object is in the Singleton model; the GetSessionManager() method
is static and the reference to the object in session is maintained.
*/
}
}
}

This is just a very small subset of the actual class, but I think my point has be made. You can easily extend this class to include typed datasets, additional properties, etc.... I now have available to me the currently logged in user’s information, the selected application’s connection string, and whatever other luggage may be necessary to carry around.

I have used this to maintain the current index of a DataGrid, handled a return to previous page, and managed the drill down into child pages. There are plenty of possibilities. This class could even be extended to allow for multiple session managers across the site, each having their own area of responsibility.



Comments

Rohan Cragg said:

Great post. Coincidentaly I just wrote my own version (all by myself) two minutes before reading this but I couldnt understand why the my setting of properties on the singleton object was 'magically' being persisted in session even though I had forgotten to re-add the object to session. That's when I did a Google search and found your post.

Have you any idea how the ASP.Net 'magically' persists these singletons? I'm surprised not to have seen an article on this before. Very useful technique anyway - I'm delighted I managed to work it out all by myself having just finished a book on patterns.
# June 10, 2005 7:10 AM

qwer said:

http://forum.lixium.fr/cgi-bin/liste.eur?amateur0

http://forum.lixium.fr/cgi-bin/liste.eur?anal0

http://forum.lixium.fr/cgi-bin/liste.eur?anime0

http://forum.lixium.fr/cgi-bin/liste.eur?asian0

http://forum.lixium.fr/cgi-bin/liste.eur?bbw0

http://forum.lixium.fr/cgi-bin/liste.eur?bdsm0

http://forum.lixium.fr/cgi-bin/liste.eur?bigboob0

http://forum.lixium.fr/cgi-bin/liste.eur?bigcock0

http://forum.lixium.fr/cgi-bin/liste.eur?bigtits0

http://forum.lixium.fr/cgi-bin/liste.eur?bisexual

http://forum.lixium.fr/cgi-bin/liste.eur?bizarre0

http://forum.lixium.fr/cgi-bin/liste.eur?black0

http://forum.lixium.fr/cgi-bin/liste.eur?blondes0

http://forum.lixium.fr/cgi-bin/liste.eur?blowjob0

http://forum.lixium.fr/cgi-bin/liste.eur?bondage0

http://forum.lixium.fr/cgi-bin/liste.eur?brunett0

http://forum.lixium.fr/cgi-bin/liste.eur?busty0

http://forum.lixium.fr/cgi-bin/liste.eur?cartoon0

http://forum.lixium.fr/cgi-bin/liste.eur?center0

http://forum.lixium.fr/cgi-bin/liste.eur?cumshot0

http://forum.lixium.fr/cgi-bin/liste.eur?doggy0

http://forum.lixium.fr/cgi-bin/liste.eur?double0

http://forum.lixium.fr/cgi-bin/liste.eur?drunkgi0

http://forum.lixium.fr/cgi-bin/liste.eur?ebony0

http://forum.lixium.fr/cgi-bin/liste.eur?ethnic0

http://forum.lixium.fr/cgi-bin/liste.eur?facial0

http://forum.lixium.fr/cgi-bin/liste.eur?fat0

http://forum.lixium.fr/cgi-bin/liste.eur?feet0

http://forum.lixium.fr/cgi-bin/liste.eur?femdom0

http://forum.lixium.fr/cgi-bin/liste.eur?fetish0

http://forum.lixium.fr/cgi-bin/liste.eur?gangban0

http://forum.lixium.fr/cgi-bin/liste.eur?gayanal0

http://forum.lixium.fr/cgi-bin/liste.eur?gayanime

http://forum.lixium.fr/cgi-bin/liste.eur?gayasia0

http://forum.lixium.fr/cgi-bin/liste.eur?gaybear0

http://forum.lixium.fr/cgi-bin/liste.eur?gayblac0

http://forum.lixium.fr/cgi-bin/liste.eur?gayblow0

http://forum.lixium.fr/cgi-bin/liste.eur?gayboy0

http://forum.lixium.fr/cgi-bin/liste.eur?gaycart0

http://forum.lixium.fr/cgi-bin/liste.eur?gaycock0

http://forum.lixium.fr/cgi-bin/liste.eur?gaycoll0

http://forum.lixium.fr/cgi-bin/liste.eur?gaycum0

http://forum.lixium.fr/cgi-bin/liste.eur?gaydati0

http://forum.lixium.fr/cgi-bin/liste.eur?gayebon0

http://forum.lixium.fr/cgi-bin/liste.eur?gaychat0

http://forum.lixium.fr/cgi-bin/liste.eur?gaysex0

http://forum.lixium.fr/cgi-bin/liste.eur?gayfuck0

http://forum.lixium.fr/cgi-bin/liste.eur?gayhairy

http://forum.lixium.fr/cgi-bin/liste.eur?gayhenta

http://forum.lixium.fr/cgi-bin/liste.eur?gayhunks

http://forum.lixium.fr/cgi-bin/liste.eur?gaylatin

http://forum.lixium.fr/cgi-bin/liste.eur?gaymale0

http://forum.lixium.fr/cgi-bin/liste.eur?gaymanga

http://forum.lixium.fr/cgi-bin/liste.eur?gaymatur

http://forum.lixium.fr/cgi-bin/liste.eur?gaymen0

http://forum.lixium.fr/cgi-bin/liste.eur?gaymovie

http://forum.lixium.fr/cgi-bin/liste.eur?gaymuscl

http://forum.lixium.fr/cgi-bin/liste.eur?gayolder

http://forum.lixium.fr/cgi-bin/liste.eur?gayperso

http://forum.lixium.fr/cgi-bin/liste.eur?gaypics0

http://forum.lixium.fr/cgi-bin/liste.eur?gayporn0

http://forum.lixium.fr/cgi-bin/liste.eur?gaysex0

http://forum.lixium.fr/cgi-bin/liste.eur?gayteen0

http://forum.lixium.fr/cgi-bin/liste.eur?gaytwink

http://forum.lixium.fr/cgi-bin/liste.eur?gayvideo

http://forum.lixium.fr/cgi-bin/liste.eur?gaywebca

http://forum.lixium.fr/cgi-bin/liste.eur?hairyman

http://forum.lixium.fr/cgi-bin/liste.eur?male0

http://forum.lixium.fr/cgi-bin/liste.eur?nakedman

http://forum.lixium.fr/cgi-bin/liste.eur?younggay

http://forum.lixium.fr/cgi-bin/liste.eur?group0

http://forum.lixium.fr/cgi-bin/liste.eur?hairygir

http://forum.lixium.fr/cgi-bin/liste.eur?handjob0

http://forum.lixium.fr/cgi-bin/liste.eur?hardsex

http://forum.lixium.fr/cgi-bin/liste.eur?hentai0

http://forum.lixium.fr/cgi-bin/liste.eur?hotgirl0

http://forum.lixium.fr/cgi-bin/liste.eur?hotguy0

http://forum.lixium.fr/cgi-bin/liste.eur?indian0

http://forum.lixium.fr/cgi-bin/liste.eur?interrac

http://forum.lixium.fr/cgi-bin/liste.eur?latino0

http://forum.lixium.fr/cgi-bin/liste.eur?legs0

http://forum.lixium.fr/cgi-bin/liste.eur?lesbian0

http://forum.lixium.fr/cgi-bin/liste.eur?live0

http://forum.lixium.fr/cgi-bin/liste.eur?manga0

http://forum.lixium.fr/cgi-bin/liste.eur?masturba

http://forum.lixium.fr/cgi-bin/liste.eur?mature0

http://forum.lixium.fr/cgi-bin/liste.eur?milf0

http://forum.lixium.fr/cgi-bin/liste.eur?olderwom

http://forum.lixium.fr/cgi-bin/liste.eur?oral0

http://forum.lixium.fr/cgi-bin/liste.eur?orgy0

http://forum.lixium.fr/cgi-bin/liste.eur?panties0

http://forum.lixium.fr/cgi-bin/liste.eur?pantyhos

http://forum.lixium.fr/cgi-bin/liste.eur?petite0

http://forum.lixium.fr/cgi-bin/liste.eur?pornmovi

http://forum.lixium.fr/cgi-bin/liste.eur?pornstar

http://forum.lixium.fr/cgi-bin/liste.eur?pornvide

http://forum.lixium.fr/cgi-bin/liste.eur?reality0

http://forum.lixium.fr/cgi-bin/liste.eur?redhead0

http://forum.lixium.fr/cgi-bin/liste.eur?sextoys0

http://forum.lixium.fr/cgi-bin/liste.eur?sexvids

http://forum.lixium.fr/cgi-bin/liste.eur?shaved0

http://forum.lixium.fr/cgi-bin/liste.eur?shemale0

http://forum.lixium.fr/cgi-bin/liste.eur?smoking0

http://forum.lixium.fr/cgi-bin/liste.eur?spanking

http://forum.lixium.fr/cgi-bin/liste.eur?teen0

http://forum.lixium.fr/cgi-bin/liste.eur?tranny0

http://forum.lixium.fr/cgi-bin/liste.eur?transsex

http://forum.lixium.fr/cgi-bin/liste.eur?upskirt0

http://forum.lixium.fr/cgi-bin/liste.eur?voyeur0

http://forum.lixium.fr/cgi-bin/liste.eur?webcams0

http://forum.lixium.fr/cgi-bin/liste.eur?young0

http://0rz.tw/782bw

http://0rz.tw/f92ac

http://0rz.tw/0628d

http://0rz.tw/ca29L

http://0rz.tw/1c29n

http://0rz.tw/1129l

http://0rz.tw/772bC

http://0rz.tw/052bu

http://0rz.tw/792cj

http://0rz.tw/8c2cW

http://0rz.tw/1f2ce

http://0rz.tw/542ai

http://0rz.tw/2429n

http://0rz.tw/be2a6

http://0rz.tw/742dN

http://0rz.tw/4c281

http://0rz.tw/622aS

http://0rz.tw/502bR

http://0rz.tw/c12aB

http://0rz.tw/2228T

http://0rz.tw/ae2bw

http://0rz.tw/a72du

http://0rz.tw/042br

http://0rz.tw/582aI

http://0rz.tw/042bs

http://0rz.tw/e52b8

http://0rz.tw/652cr

http://0rz.tw/ac2dF

http://0rz.tw/192b6

http://0rz.tw/5329g

http://0rz.tw/c12aC

http://0rz.tw/1c29o

http://0rz.tw/b12a1

http://0rz.tw/5e2bi

http://0rz.tw/f429K

http://0rz.tw/162aa

http://0rz.tw/3b2bF

http://0rz.tw/ac2dG

http://0rz.tw/9128t

http://0rz.tw/a52c3

http://0rz.tw/3428c

http://0rz.tw/552b0

http://0rz.tw/832a8

http://0rz.tw/0a2d9

http://0rz.tw/d02ad

http://0rz.tw/2d2cI

http://0rz.tw/9629A

http://0rz.tw/6c2cg

http://0rz.tw/8a28l

http://0rz.tw/702be

http://0rz.tw/a42b8

http://0rz.tw/6029l

http://0rz.tw/5b2bv

http://0rz.tw/b52b9

http://0rz.tw/082cu

http://0rz.tw/f22aH

http://0rz.tw/312bn

http://0rz.tw/b32aZ

http://0rz.tw/1129m

http://0rz.tw/512b1

http://0rz.tw/422cp

http://0rz.tw/6e29a

http://0rz.tw/6029m

http://0rz.tw/852a7

http://0rz.tw/9329W

http://0rz.tw/de2bz

http://0rz.tw/192b7

http://0rz.tw/f727x

http://0rz.tw/392aZ

http://0rz.tw/ed2cv

http://0rz.tw/632ak

http://0rz.tw/a72dw

http://0rz.tw/ed2cx

http://0rz.tw/bd29V

http://0rz.tw/b229s

http://0rz.tw/4327i

http://0rz.tw/902cg

http://0rz.tw/cc2bc

http://0rz.tw/aa2aG

http://0rz.tw/472ce

http://0rz.tw/d628Q

http://0rz.tw/f12a5

http://0rz.tw/5929r

http://0rz.tw/8d2ac

http://0rz.tw/d127X

http://0rz.tw/8d2ad

http://0rz.tw/fa2bq

http://0rz.tw/522bq

http://0rz.tw/4a29S

http://0rz.tw/e42c7

http://0rz.tw/d628R

http://0rz.tw/162ab

http://0rz.tw/1e2ag

http://0rz.tw/582aJ

http://0rz.tw/ea29z

http://0rz.tw/162ac

http://0rz.tw/3428d

http://0rz.tw/7e2a4

http://0rz.tw/b02ao

http://0rz.tw/282aS

http://0rz.tw/cf2cq

http://0rz.tw/902ch

http://0rz.tw/cc2bd

http://0rz.tw/b02ap

http://0rz.tw/582aK

http://0rz.tw/bd29W

http://0rz.tw/472cf

http://0rz.tw/0e2ap

http://0rz.tw/6a2cc

http://0rz.tw/7a2at

http://0rz.tw/9128w

http://relurl.com/K52

http://relurl.com/L52

http://relurl.com/M52

http://relurl.com/N52

http://relurl.com/B52

http://relurl.com/V52

http://relurl.com/C52

http://relurl.com/X52

http://relurl.com/Z52

http://relurl.com/162

http://relurl.com/262

http://relurl.com/362

http://relurl.com/462

http://relurl.com/562

http://relurl.com/662

http://relurl.com/762

http://relurl.com/862

http://relurl.com/962

http://relurl.com/062

http://relurl.com/p62

http://relurl.com/o62

http://relurl.com/i62

http://relurl.com/u62

http://relurl.com/y62

http://relurl.com/t62

http://relurl.com/r62

http://relurl.com/w62

http://relurl.com/q62

http://relurl.com/a62

http://relurl.com/s62

http://relurl.com/d62

http://relurl.com/f62

http://relurl.com/g62

http://relurl.com/h62

http://relurl.com/j62

http://relurl.com/k62

http://relurl.com/l62

http://relurl.com/m62

http://relurl.com/n62

http://relurl.com/b62

http://relurl.com/v62

http://relurl.com/c62

http://relurl.com/x62

http://relurl.com/z62

http://relurl.com/P62

http://relurl.com/O62

http://relurl.com/I62

http://relurl.com/U62

http://relurl.com/Y62

http://relurl.com/T62

http://relurl.com/R62

http://relurl.com/E62

http://relurl.com/W62

http://relurl.com/Q62

http://relurl.com/A62

http://relurl.com/S62

http://relurl.com/D62

http://relurl.com/F62

http://relurl.com/G62

http://relurl.com/H62

http://relurl.com/J62

http://relurl.com/K62

http://relurl.com/L62

http://relurl.com/M62

http://relurl.com/N62

http://relurl.com/B62

http://relurl.com/V62

http://relurl.com/C62

http://relurl.com/X62

http://relurl.com/Z62

http://relurl.com/172

http://relurl.com/272

http://relurl.com/372

http://relurl.com/472

http://relurl.com/572

http://relurl.com/672

http://relurl.com/772

http://relurl.com/872

http://relurl.com/972

http://relurl.com/072

http://relurl.com/p72

http://relurl.com/o72

http://relurl.com/i72

http://relurl.com/u72

http://relurl.com/y72

http://relurl.com/t72

http://relurl.com/r72

http://relurl.com/e72

http://relurl.com/w72

http://relurl.com/q72

http://relurl.com/a72

http://relurl.com/s72

http://relurl.com/d72

http://relurl.com/f72

http://relurl.com/g72

http://relurl.com/h72

http://relurl.com/j72

http://relurl.com/k72

http://relurl.com/l72

http://relurl.com/m72

http://relurl.com/n72

http://relurl.com/b72

http://relurl.com/v72

http://relurl.com/v72

http://relurl.com/c72

http://relurl.com/x72

http://relurl.com/z72

http://relurl.com/P72

http://relurl.com/O72

http://relurl.com/I72

http://relurl.com/Y72

# December 16, 2006 4:09 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

Our Sponsors

This Blog

Syndication

News