Click here to Skip to main content
Licence CPOL
First Posted 11 Jan 2011
Views 13,456
Bookmarked 6 times

Programming Practice for Server Side State Maintenance Variable

By | 11 Jan 2011 | Article
Programming practice for server side state maintenance variable

Introduction

Http protocol is stateless, so we need to maintain state on Client side or Server side. For this time, I am going to discuss about the maintaining state on Server Side with one programming practice.

Problem

To maintain state on Server side, most of the developers use Session, Caching, Application variables. But most of the beginner developers make one mistake, which is:

session["myData"] = data;

Code pushes data in session variable(s).

Data = (Conversion_To_Data_Type_Of_Data) session["mydata"];

Code gets data back from session variable.So most of the developers follow the above procedure to push and get data from server side variable.

Disadvantages

  1. Developer requires remembering name of string value, i.e., name of the variable. If you refer to the above code, it's mydata.
  2. If there are a number of developers working on a project, it’s hard to maintain server side variable if we follow the above procedure, i.e., your maintenance increases.
  3. If there are large number of developers working in your project, you require to inform each and every one about the variable you are declaring.

Solution

The easiest solution that I found after doing so much programming is as follows:

Step 1

Create one static class:

public class SessionPropeties
{
 // code
}

Step 2

Create property for each session variable of your application as shown below:

public int UserId
{
        get
        {
           if(HttpContext.Current.Session["UserID"]!=null)
            return Convert.ToInt32(HttpContext.Current.Session["UserID"].ToString());
           else
          return 0;
        }
        set
        {
            HttpContext.Current.Session["UserID"] = value;
        }
}

After following the above steps, the class will be like below:

public class SessionPropeties
{
 public DataType prop1
 {
        get
        {
           if(HttpContext.Current.Session["prop1"]!=null)
            return Convert.ToDataType (HttpContext.Current.Session["prop1"].ToString());
           else
            return defaultvalue;
        }
        set
        {
            HttpContext.Current.Session["prop1"] = value;
        }
      
 }
 public DataType prop2
 {
        get
        {
           if(HttpContext.Current.Session["prop2"]!=null)
            return Convert.ToDataType (HttpContext.Current.Session["prop2"].ToString());
           else
            return defaultvalue;
        }
        set
        {
            HttpContext.Current.Session["prop2"] = value;
        }
      
 }
 
 ...........
 
}

Step 3

In coding to get and set data, you just require to call these properties.

To set data
SessionProperties.UserId = 1;
To get data
int userid = SessionProperties.UserId;

Advantages

  1. Once you declare your properties, you do not need to remember it. You just need to call your Session property class.
  2. Maintenance becomes easy when project size is large or there are a number of developers working in a team, because you just need to refer to the Sessionproperties class having all severside properties. And you can also add property you need and you don’t require to inform all the people working in your team.

Summary

You can maintain your Cache and Application server side variable in a similar way. So it's better you encapsulate your server side variable in one class which makes your task easy when you do code in web application.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Pranay Rana

Software Developer (Senior)
GMind Solusion
India India

Member

Follow on Twitter Follow on Twitter
Hey, I am Pranay Rana, working as a ITA in MNC. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 5.5 years now.
 
For me def. of programming is : Programming is something that you do once and that get used by multiple for many years
 
You can visit me on my blog - http://pranayamr.blogspot.com/
StackOverFlow - http://stackoverflow.com/users/314488/pranay
My CV :- http://careers.stackoverflow.com/pranayamr
 
Awards:




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
GeneralAll objects into a single Class PinmemberYves14:59 17 Jan '11  
GeneralMy vote of 1 Pinmemberaprishchepov10:33 13 Jan '11  
GeneralRe: My vote of 1 PinmemberPranay Rana7:22 14 Jan '11  
GeneralMy vote of 4 PinmemberPravin Patil, Mumbai3:15 12 Jan '11  
GeneralRe: My vote of 4 PinmemberPranay Rana5:58 12 Jan '11  
GeneralMy vote of 4 PinmemberRuchit S.20:12 11 Jan '11  
GeneralRe: My vote of 4 PinmemberPranay Rana20:14 11 Jan '11  
GeneralGood job! PinmemberVenkatesh Mookkan19:52 11 Jan '11  
GeneralRe: Good job! PinmemberPranay Rana19:58 11 Jan '11  
GeneralMy vote of 4 PinmemberKrunal Mevada18:48 11 Jan '11  
GeneralRe: My vote of 4 PinmemberPranay Rana20:06 11 Jan '11  
GeneralBetter design.. PinmemberRuchit S.18:43 11 Jan '11  
GeneralRe: Better design.. PinmemberVenkatesh Mookkan19:57 11 Jan '11  
GeneralRe: Better design.. PinmemberRuchit S.20:11 11 Jan '11  
GeneralThoughts PinmemberPIEBALDconsult18:13 11 Jan '11  
GeneralRe: Thoughts PinmemberPranay Rana18:33 11 Jan '11  
GeneralRe: Thoughts PinmemberPIEBALDconsult2:28 12 Jan '11  
GeneralRe: Thoughts PinmemberPranay Rana8:55 13 Jan '11  
GeneralRe: Thoughts PinmemberDstructr21:17 17 Jan '11  

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
Web02 | 2.5.120517.1 | Last Updated 11 Jan 2011
Article Copyright 2011 by Pranay Rana
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid