Click here to Skip to main content
Click here to Skip to main content

Exposing the Session Object as a User Defined Class

, 17 Apr 2003
Rate this:
Please Sign up or sign in to vote.
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:

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:

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

Here is the example SessionSink

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

Share

About the Author

Simon Segal
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.... PinmemberSimon Segal20-Jun-08 22:37 
GeneralFind me here.... PinmemberSimon Segal20-Jun-08 22:36 
QuestionWhat happens when two browsers are using the same session object? Pinmember121_C_Sharper9-Aug-07 2:11 
QuestionQuick syntax question PinmembermeLecrone26-Jan-06 7:50 
AnswerRe: Quick syntax question PinmemberSimon Segal26-Jan-06 12:16 
GeneralSmall World Pinmemberamericanbaldeagle26-Aug-04 19:51 
GeneralMake it serializable PinmemberYao Zhifeng22-Mar-04 22:09 
GeneralRe: Make it serializable PinmemberSimon Segal22-Mar-04 22:29 
GeneralUsing Session Object in VB PinmemberGeogie1-Aug-03 7:26 
GeneralRe: Using Session Object in VB PinmemberSimon Segal3-Aug-03 14:20 
GeneralRe: Using Session Object in VB PinmemberGeogie4-Aug-03 12:13 
GeneralSession objects... PinmembertheJazzyBrain3-Jul-03 4:57 
GeneralRe: Session objects... PinmemberSimon Segal6-Jul-03 20:44 
GeneralRe: Session objects... PinmembertheJazzyBrain7-Jul-03 3:18 
GeneralRe: Session objects... PinmemberSimon Segal7-Jul-03 3:24 
GeneralRe: Session objects... PinmembertheJazzyBrain7-Jul-03 3:28 
QuestionHow to use in VB.Net Pinmemberjbahri11-Jun-03 2:50 
AnswerRe: How to use in VB.Net PinmemberSimon Segal11-Jun-03 14:20 
GeneralRe: How to use in VB.Net Pinmemberjbahri11-Jun-03 19:21 
GeneralRe: How to use in VB.Net PinmemberSimon Segal11-Jun-03 23:47 
GeneralRe: How to use in VB.Net Pinmemberjbahri12-Jun-03 0:06 
GeneralRe: How to use in VB.Net PinmemberSimon Segal12-Jun-03 1:49 
GeneralRe: How to use in VB.Net Pinmemberjbahri12-Jun-03 2:10 
GeneralRe: How to use in VB.Net PinmemberSimon Segal12-Jun-03 2:48 
GeneralRe: How to use in VB.Net Pinmemberpowiz19-Jan-04 19:06 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 18 Apr 2003
Article Copyright 2003 by Simon Segal
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid