Click here to Skip to main content
12,397,380 members (61,573 online)
Click here to Skip to main content
Add your own
alternative version

Stats

183.8K views
41 bookmarked
Posted

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.

You may also be interested in...

Comments and Discussions

 
GeneralFind me here.... Pin
Simon Segal20-Jun-08 21:37
memberSimon Segal20-Jun-08 21:37 
GeneralFind me here.... Pin
Simon Segal20-Jun-08 21:36
memberSimon Segal20-Jun-08 21:36 
QuestionWhat happens when two browsers are using the same session object? Pin
121_C_Sharper9-Aug-07 1:11
member121_C_Sharper9-Aug-07 1:11 
QuestionQuick syntax question Pin
meLecrone26-Jan-06 6:50
membermeLecrone26-Jan-06 6:50 
AnswerRe: Quick syntax question Pin
Simon Segal26-Jan-06 11:16
memberSimon Segal26-Jan-06 11:16 
GeneralSmall World Pin
americanbaldeagle26-Aug-04 18:51
memberamericanbaldeagle26-Aug-04 18:51 
GeneralMake it serializable Pin
Yao Zhifeng22-Mar-04 21:09
memberYao Zhifeng22-Mar-04 21:09 
GeneralRe: Make it serializable Pin
Simon Segal22-Mar-04 21:29
memberSimon Segal22-Mar-04 21:29 
GeneralUsing Session Object in VB Pin
Geogie1-Aug-03 6:26
memberGeogie1-Aug-03 6:26 
GeneralRe: Using Session Object in VB Pin
Simon Segal3-Aug-03 13:20
memberSimon Segal3-Aug-03 13:20 
GeneralRe: Using Session Object in VB Pin
Geogie4-Aug-03 11:13
memberGeogie4-Aug-03 11:13 
GeneralSession objects... Pin
theJazzyBrain3-Jul-03 3:57
membertheJazzyBrain3-Jul-03 3:57 
GeneralRe: Session objects... Pin
Simon Segal6-Jul-03 19:44
memberSimon Segal6-Jul-03 19:44 
GeneralRe: Session objects... Pin
theJazzyBrain7-Jul-03 2:18
membertheJazzyBrain7-Jul-03 2:18 
GeneralRe: Session objects... Pin
Simon Segal7-Jul-03 2:24
memberSimon Segal7-Jul-03 2:24 
GeneralRe: Session objects... Pin
theJazzyBrain7-Jul-03 2:28
membertheJazzyBrain7-Jul-03 2:28 
QuestionHow to use in VB.Net Pin
jbahri11-Jun-03 1:50
memberjbahri11-Jun-03 1:50 
AnswerRe: How to use in VB.Net Pin
Simon Segal11-Jun-03 13:20
memberSimon Segal11-Jun-03 13:20 
GeneralRe: How to use in VB.Net Pin
jbahri11-Jun-03 18:21
memberjbahri11-Jun-03 18:21 
GeneralRe: How to use in VB.Net Pin
Simon Segal11-Jun-03 22:47
memberSimon Segal11-Jun-03 22:47 
GeneralRe: How to use in VB.Net Pin
jbahri11-Jun-03 23:06
memberjbahri11-Jun-03 23:06 
GeneralRe: How to use in VB.Net Pin
Simon Segal12-Jun-03 0:49
memberSimon Segal12-Jun-03 0:49 
GeneralRe: How to use in VB.Net Pin
jbahri12-Jun-03 1:10
memberjbahri12-Jun-03 1:10 
GeneralRe: How to use in VB.Net Pin
Simon Segal12-Jun-03 1:48
memberSimon Segal12-Jun-03 1:48 
GeneralRe: How to use in VB.Net Pin
powiz19-Jan-04 18:06
memberpowiz19-Jan-04 18:06 
GeneralRe: How to use in VB.Net Pin
Simon Segal19-Jan-04 22:21
memberSimon Segal19-Jan-04 22:21 
GeneralRe: How to use in VB.Net Pin
powiz19-Jan-04 22:36
memberpowiz19-Jan-04 22:36 
GeneralRe: How to use in VB.Net Pin
Simon Segal19-Jan-04 23:13
memberSimon Segal19-Jan-04 23:13 
GeneralRe: How to use in VB.Net Pin
PeterSmith6-Apr-05 2:56
memberPeterSmith6-Apr-05 2:56 
GeneralRe: How to use in VB.Net Pin
Simon Segal6-Apr-05 13:21
memberSimon Segal6-Apr-05 13:21 
GeneralProblems using this when storing session state in SQL Server Pin
mikemalter21-Apr-03 6:23
membermikemalter21-Apr-03 6:23 
GeneralRe: Problems using this when storing session state in SQL Server Pin
Simon Segal25-Apr-03 0:37
memberSimon Segal25-Apr-03 0:37 
GeneralRe: Problems using this when storing session state in SQL Server Pin
mikemalter25-Apr-03 4:57
membermikemalter25-Apr-03 4:57 
GeneralRe: Problems using this when storing session state in SQL Server Pin
mikemalter25-Apr-03 8:32
membermikemalter25-Apr-03 8:32 
GeneralRe: Problems using this when storing session state in a stand alone class Pin
purushdeo12-May-04 8:43
susspurushdeo12-May-04 8:43 
GeneralRe: Problems using this when storing session state in a stand alone class Pin
Simon Segal12-May-04 13:45
memberSimon Segal12-May-04 13:45 
GeneralHandy concept but flawed Implementation. Pin
Pete Bassett29-Mar-03 0:22
memberPete Bassett29-Mar-03 0:22 
GeneralRe: Handy concept but flawed Implementation. Pin
leppie29-Mar-03 4:47
memberleppie29-Mar-03 4:47 
GeneralRe: Handy concept but flawed Implementation. Pin
Richard Deeming30-Mar-03 21:54
memberRichard Deeming30-Mar-03 21:54 
GeneralRe: Handy concept but flawed Implementation. Pin
Pete Bassett30-Mar-03 22:33
memberPete Bassett30-Mar-03 22:33 
GeneralRe: Handy concept but flawed Implementation. Pin
Simon Segal29-Mar-03 23:00
memberSimon Segal29-Mar-03 23:00 
GeneralRe: Handy concept but flawed Implementation. Pin
Pete Bassett30-Mar-03 22:31
memberPete Bassett30-Mar-03 22:31 
GeneralRe: Handy concept but flawed Implementation. Pin
Simon Segal30-Mar-03 22:44
memberSimon Segal30-Mar-03 22:44 
GeneralRe: Handy concept but flawed Implementation. Pin
marcpgarcia1-Apr-03 12:15
membermarcpgarcia1-Apr-03 12:15 
GeneralRe: Handy concept but flawed Implementation. Pin
Pete Bassett1-Apr-03 23:54
memberPete Bassett1-Apr-03 23:54 
GeneralRe: Handy concept but flawed Implementation. Pin
marcpgarcia2-Apr-03 0:43
membermarcpgarcia2-Apr-03 0:43 
GeneralRe: Handy concept but flawed Implementation. Pin
Pete Bassett2-Apr-03 1:03
memberPete Bassett2-Apr-03 1:03 
GeneralRe: Handy concept but flawed Implementation. Pin
marcpgarcia2-Apr-03 6:56
membermarcpgarcia2-Apr-03 6:56 
GeneralRe: Handy concept but flawed Implementation. Pin
RSATUR1-Apr-03 14:59
memberRSATUR1-Apr-03 14:59 
GeneralRe: Handy concept but flawed Implementation. Pin
Pete Bassett2-Apr-03 0:00
memberPete Bassett2-Apr-03 0:00 

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.

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