Sunday, January 9, 2011

Programming practice for server side state maintenance variable

Shout it kick it on DotNetKicks.com
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 developer use Session, Caching, Application variables. But most of the beginner developer does 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 above procedure to push and get data from server side variable.

Disadvantage
  • Developer requires remembering name of string value i.e name of the variable. If you refer above code its mydata.
  • If no. of developer working on project, it’s hard to maintain server side variable if we follow above procedure. i.e you maintenance increase.
  • If there are large no developer 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 following

Step 1
Create one Static class
public class SessionPropeties
{
  ….. code 
}
Step 2
Create property for each session variable of your application as show 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 above steps class will be like as 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 just require to call this properties
To set data
SessionProperties.UserId = 1;
To get data
int userid = SessionProperties.UserId;
Above implementation is somewhat similar to singleton pattern because as you see there is only one instance of class as all properties in it are static. But the real thing is the all are pointing to session variable of the user session.

Advantages
  • Once you declare your properties you do not need to remember it. You just need to call your Session property class.
  • Maintenance becomes easy when project size is large or no. of developer working in team, because you just need to refer Sessionproperties class having all severside properties. And you can also add property you need and you don’t require informing all the people working in your team.
Summary
You can maintain your Cache and Application server side variable in similar way. So its better you encapsulate your server side variable in on class which makes your task easy when you do code in web application.

2 comments:

  1. In a web farm, that class will be static on each box of the web farm. Single server this solution works.

    ReplyDelete
  2. very nice solution..keep posting this kind of stuff..

    thanks again..

    ReplyDelete