Click here to Skip to main content
15,867,991 members
Articles / Web Development / ASP.NET
Article

Exposing the Session Object as a User Defined Class

Rate me:
Please Sign up or sign in to vote.
4.24/5 (20 votes)
17 Apr 2003 216.2K   41   58
A simple way to encapsulate all Session object variables in a user defined class

Introduction

It has been known to happen that, values strored in the Session object can become numerous enough to be unwieldy, ambiguous and often developers will just plain forget what they have stored there. Recently I decided to simplify the matter for myself and began to use an object that I named a SessionSink and placed all session values in the sink object rather than having (n) number of variables making for clutter and difficulty in management. By using the SessionSink we can encapsulate all Session values into one object stored in the Session object. Furthermore we are able to use all the encapsulation practices afforded to us by wrapping it all up in one single user defined class.

The code below is an example of one such SessionSink and can be referred to in code as per the following code snippet:

C#
private void someMethod()
{
    ((SessionSink)(Session["SessionSink"])).GetSetSiteLocationId = 1;
}

Making sure you cast the sink object will give you access to all it’s publicly exposed members. You can initialise the sink in the global.asax Session_Start event method like this:

C#
protected void Session_Start(Object sender, EventArgs e)
{
    Session["SessionSink"] = new SessionSink();
}

Here is the example SessionSink

C#
using System;
using System.Data.SqlClient;
namespace MySessionSink
{
    public class SessionSink
    {
        //Hide these and expose with accessor mutators
        private bool init;
        private string user;
        private string pwd;
        private int CoLocationId;
        private int SiteLocationId;
        private string SlaDepot;
        private int SlaDepotDistance;
        private int RegionRadiusId;
        private string pCode;

        public SessionSink()
        {
            this.init = true;
        }
        //Re-set all the session values
        
        public void Refresh()
        {
            this.pCode = "";
            this.RegionRadiusId = 0;
            this.SlaDepotDistance = 0;
            this.SlaDepot = "";
            this.SiteLocationId = 0;
            this.CoLocationId = 0;
            this.AddUpdateSite = false;
            this.AddUpdateCompany = false;
            this.init = false;
        }

        public string GetSetPassword
        {
            get
            {
                return this.pwd;
            }
            set
            {
                this.pwd = value;
            }
        }

        public string GetSetUserName
        {
            get
            {
                return this.user;
            }
            set
            {
                this.user = value;
            }
        }

        public string GetSetPcode
        {
            get
            {
                return this.pCode;
            }
            set
            {
                this.pCode = value;
            }
        }

        public int GetSetRegionRadiusId
        {
            get
            {
                return this.RegionRadiusId;
            }
            set
            {
                this.RegionRadiusId = value;
            }
        }

        public int GetSetDepotDistance
        {
            get
            {
                return this.SlaDepotDistance;
            }
            set
            {
                this.SlaDepotDistance = value;
            }
        }

        public string GetSetRepairDepot
        {
            get
            {
                return this.SlaDepot;
            }
            set
            {
                this.SlaDepot = value;
            }
        }

        public int GetSetSiteLocationId
        {
            get
            {
                return this.SiteLocationId;
            }
            set
            {
                this.SiteLocationId = value;
            }
        }

        public int GetSetCoLocationId
        {
            get
            {
                return this.CoLocationId;
            }
            set
            {
                this.CoLocationId = value;
            }
        }

        public bool initStatus
        {
            get
            {
                return this.init;
            }
        }
    }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Chief Technology Officer
Australia Australia
Simon Segal resides in Melbourne Australia, is a certified MCAD, MCSD, MCDBA, MCSE, MCST BizTalk Specialist and has been working in the Software Development industry for some 10 years now. His key area of interest are distributed systems / SOA built with Microsoft technologies.

Comments and Discussions

 
GeneralRe: Handy concept but flawed Implementation. Pin
Pete Bassett30-Mar-03 22:31
Pete Bassett30-Mar-03 22:31 
GeneralRe: Handy concept but flawed Implementation. Pin
Simon Segal30-Mar-03 22:44
Simon Segal30-Mar-03 22:44 
GeneralRe: Handy concept but flawed Implementation. Pin
Marc Garcia1-Apr-03 12:15
Marc Garcia1-Apr-03 12:15 
GeneralRe: Handy concept but flawed Implementation. Pin
Pete Bassett1-Apr-03 23:54
Pete Bassett1-Apr-03 23:54 
GeneralRe: Handy concept but flawed Implementation. Pin
Marc Garcia2-Apr-03 0:43
Marc Garcia2-Apr-03 0:43 
GeneralRe: Handy concept but flawed Implementation. Pin
Pete Bassett2-Apr-03 1:03
Pete Bassett2-Apr-03 1:03 
GeneralRe: Handy concept but flawed Implementation. Pin
Marc Garcia2-Apr-03 6:56
Marc Garcia2-Apr-03 6:56 
GeneralRe: Handy concept but flawed Implementation. Pin
bobsvillia1-Apr-03 14:59
bobsvillia1-Apr-03 14:59 
I too have used this idea many times in various projects and after reading the email thread I have only one comment I would like to add. Aside from the style and usability improvements already correctly stated, the usage proposed by Simon is certainly not flawed, unless someone can point out any adverse behaviour that may result from using this particular implementation. Hence the reference to it being a "flawed implementation" is unnecessary and way too strong a comment. I would like to continue to encourage new contributors because I am learning quite a lot from this site. Well done Simon, nice article first up. Pete this was not an attack on you personally but I think the subject title was a little heavy handed, and I do agree with your improvements. Out of curosity can anyone find any possible unwanted adverse behaviour from doing it this way?Smile | :)

Richard Satur
rsatur@optusnet.com.au
GeneralRe: Handy concept but flawed Implementation. Pin
Pete Bassett2-Apr-03 0:00
Pete Bassett2-Apr-03 0:00 
GeneralStill NOT flawed by definition Pin
Simon Segal6-Apr-03 19:32
Simon Segal6-Apr-03 19:32 
GeneralRe: Still NOT flawed by definition Pin
Pete Bassett7-Apr-03 1:44
Pete Bassett7-Apr-03 1:44 
GeneralRe: Still NOT flawed by definition Pin
Simon Segal7-Apr-03 15:19
Simon Segal7-Apr-03 15:19 
GeneralRe: Still NOT flawed by definition Pin
Pete Bassett8-Apr-03 1:06
Pete Bassett8-Apr-03 1:06 
GeneralRe: Handy concept but flawed Implementation. Pin
John Giblin24-Jul-03 4:24
John Giblin24-Jul-03 4:24 
GeneralRe: Handy concept but flawed Implementation. Pin
Simon Segal24-Jul-03 13:52
Simon Segal24-Jul-03 13:52 
GeneralRe: Handy concept but flawed Implementation. Pin
John Giblin29-Jul-03 8:19
John Giblin29-Jul-03 8:19 
GeneralRe: Handy concept but flawed Implementation. Pin
Simon Segal29-Jul-03 13:13
Simon Segal29-Jul-03 13:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.