Click here to Skip to main content
15,867,704 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

 
GeneralFind me here.... Pin
Simon Segal20-Jun-08 21:37
Simon Segal20-Jun-08 21:37 
GeneralFind me here.... Pin
Simon Segal20-Jun-08 21:36
Simon Segal20-Jun-08 21:36 
QuestionWhat happens when two browsers are using the same session object? Pin
121_C_Sharper9-Aug-07 1:11
121_C_Sharper9-Aug-07 1:11 
QuestionQuick syntax question Pin
meLecrone26-Jan-06 6:50
meLecrone26-Jan-06 6:50 
AnswerRe: Quick syntax question Pin
Simon Segal26-Jan-06 11:16
Simon Segal26-Jan-06 11:16 
GeneralSmall World Pin
americanbaldeagle26-Aug-04 18:51
americanbaldeagle26-Aug-04 18:51 
GeneralMake it serializable Pin
Neil Yao22-Mar-04 21:09
Neil Yao22-Mar-04 21:09 
Good idea, very OOSmile | :)
One suggestion: make it serializable by inherited from ISerializable, so it can support SQL server session store or state server.

And, I would like call this class a state class. If session is not allowed, you can even persist it using viewstate. I will use this method in my next project, thank you.
GeneralRe: Make it serializable Pin
Simon Segal22-Mar-04 21:29
Simon Segal22-Mar-04 21:29 
GeneralUsing Session Object in VB Pin
Geogie1-Aug-03 6:26
Geogie1-Aug-03 6:26 
GeneralRe: Using Session Object in VB Pin
Simon Segal3-Aug-03 13:20
Simon Segal3-Aug-03 13:20 
GeneralRe: Using Session Object in VB Pin
Geogie4-Aug-03 11:13
Geogie4-Aug-03 11:13 
GeneralSession objects... Pin
theJazzyBrain3-Jul-03 3:57
theJazzyBrain3-Jul-03 3:57 
GeneralRe: Session objects... Pin
Simon Segal6-Jul-03 19:44
Simon Segal6-Jul-03 19:44 
GeneralRe: Session objects... Pin
theJazzyBrain7-Jul-03 2:18
theJazzyBrain7-Jul-03 2:18 
GeneralRe: Session objects... Pin
Simon Segal7-Jul-03 2:24
Simon Segal7-Jul-03 2:24 
GeneralRe: Session objects... Pin
theJazzyBrain7-Jul-03 2:28
theJazzyBrain7-Jul-03 2:28 
QuestionHow to use in VB.Net Pin
jbahri11-Jun-03 1:50
jbahri11-Jun-03 1:50 
AnswerRe: How to use in VB.Net Pin
Simon Segal11-Jun-03 13:20
Simon Segal11-Jun-03 13:20 
GeneralRe: How to use in VB.Net Pin
jbahri11-Jun-03 18:21
jbahri11-Jun-03 18:21 
GeneralRe: How to use in VB.Net Pin
Simon Segal11-Jun-03 22:47
Simon Segal11-Jun-03 22:47 
GeneralRe: How to use in VB.Net Pin
jbahri11-Jun-03 23:06
jbahri11-Jun-03 23:06 
GeneralRe: How to use in VB.Net Pin
Simon Segal12-Jun-03 0:49
Simon Segal12-Jun-03 0:49 
GeneralRe: How to use in VB.Net Pin
jbahri12-Jun-03 1:10
jbahri12-Jun-03 1:10 
GeneralRe: How to use in VB.Net Pin
Simon Segal12-Jun-03 1:48
Simon Segal12-Jun-03 1:48 
GeneralRe: How to use in VB.Net Pin
powiz19-Jan-04 18:06
powiz19-Jan-04 18:06 

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.