Centralized Session Handling in ASP.Net by Class

Session management is a very common technique in web application development. We know that it's one of the most secure ways to exchange data because session data passes in an encrypted format from the client to the server and it's very secure.

In spite of the advantages, sessions may cause a few bottlenecks of applications, like session data stored in server memory and passed through every request and response. Now, if there is a huge number of session variable in the application then it might result in a shortage of server memory when a huge number of clients access the application.

And another great problem that may occur in a session is the key identity problem. For example, a project might have many modules and each module is being developed by separate developers, so there is the chance of two developers using the same key to store a session variable and after integration of modules it will be a big problem, or sometimes there might be a limitation of session variables in an application.

So, those are scenarios where a session might create a problem for both an application and a developer. But, don't lose hope. There is a solution for that. We can centralize session handling in the application. Have a look at the following code.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. namespace WebAPP  
  6. {  
  7.     public class CentralSession  
  8.     {  
  9.         public string UserId  
  10.         {  
  11.             get  
  12.             {  
  13.               return HttpContext.Current.Session["User"] == null ?  
  14.                   null :HttpContext.Current.Session["User"].ToString();  
  15.             }  
  16.             set  
  17.             {  
  18.                 HttpContext.Current.Session["User"] = value;  
  19.             }  
  20.         }  
  21.         public int ItemInCart  
  22.         {  
  23.             get  
  24.             {  
  25.                 return  HttpContext.Current.Session["Items"]== null ?  
  26.                     0 :Convert.ToInt32( HttpContext.Current.Session["Items"]);  
  27.             }  
  28.             set  
  29.             {  
  30.                 HttpContext.Current.Session["Items"] = value;  
  31.             }  
  32.         }  
  33.         public Object SessionMain  
  34.         {  
  35.             get  
  36.             {  
  37.                 return HttpContext.Current.Session["MasterUser"] == null ?  
  38.                     null : HttpContext.Current.Session["MasterUser"];  
  39.             }  
  40.             set  
  41.             {  
  42.                 HttpContext.Current.Session["MasterUser"] = value;  
  43.             }  
  44.         }  
  45.     }  
  46. } 
The implementation is very simple; we have just created one class that will maintain all the session data. The class contains a few properties and each property is responsible for one session variable and since all the variables are in a central position we can control the number of session variables created here and there.

And when we wish to access the session variables we can access then by creating an object of the class like the following.

  1. public partial class Client : System.Web.UI.Page  
  2. {  
  3.     protected void Page_Load(object sender, EventArgs e)  
  4.     {  
  5.         CentralSession Obj = new CentralSession();  
  6.         Obj.UserId = "123";  
  7.         Obj.ItemInCart = 1;  
  8.         Obj.SessionMain = Obj;  
  9.     }  
  10. } 

This is one approach to control session variable creation in an application and also implement a unique naming convention of session variable keys.


Similar Articles