Click here to Skip to main content
Licence 
First Posted 28 Mar 2003
Views 159,879
Bookmarked 41 times

Exposing the Session Object as a User Defined Class

By | 17 Apr 2003 | Article
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

About the Author

Simon Segal

Chief Technology Officer

Australia Australia

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralFind me here.... PinmemberSimon Segal21:37 20 Jun '08  
GeneralFind me here.... PinmemberSimon Segal21:36 20 Jun '08  
QuestionWhat happens when two browsers are using the same session object? Pinmember121_C_Sharper1:11 9 Aug '07  
QuestionQuick syntax question PinmembermeLecrone6:50 26 Jan '06  
AnswerRe: Quick syntax question PinmemberSimon Segal11:16 26 Jan '06  
GeneralSmall World Pinmemberamericanbaldeagle18:51 26 Aug '04  
GeneralMake it serializable PinmemberYao Zhifeng21:09 22 Mar '04  
GeneralRe: Make it serializable PinmemberSimon Segal21:29 22 Mar '04  
GeneralUsing Session Object in VB PinmemberGeogie6:26 1 Aug '03  
GeneralRe: Using Session Object in VB PinmemberSimon Segal13:20 3 Aug '03  
GeneralRe: Using Session Object in VB PinmemberGeogie11:13 4 Aug '03  
GeneralSession objects... PinmembertheJazzyBrain3:57 3 Jul '03  
GeneralRe: Session objects... PinmemberSimon Segal19:44 6 Jul '03  
GeneralRe: Session objects... PinmembertheJazzyBrain2:18 7 Jul '03  
GeneralRe: Session objects... PinmemberSimon Segal2:24 7 Jul '03  
GeneralRe: Session objects... PinmembertheJazzyBrain2:28 7 Jul '03  
QuestionHow to use in VB.Net Pinmemberjbahri1:50 11 Jun '03  
AnswerRe: How to use in VB.Net PinmemberSimon Segal13:20 11 Jun '03  
GeneralRe: How to use in VB.Net Pinmemberjbahri18:21 11 Jun '03  
GeneralRe: How to use in VB.Net PinmemberSimon Segal22:47 11 Jun '03  
GeneralRe: How to use in VB.Net Pinmemberjbahri23:06 11 Jun '03  
GeneralRe: How to use in VB.Net PinmemberSimon Segal0:49 12 Jun '03  
GeneralRe: How to use in VB.Net Pinmemberjbahri1:10 12 Jun '03  
GeneralRe: How to use in VB.Net PinmemberSimon Segal1:48 12 Jun '03  
GeneralRe: How to use in VB.Net Pinmemberpowiz18:06 19 Jan '04  

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.

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