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 applicationd, 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.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

 

namespace WebAPP

{

    public class CentralSession

    {

        public string UserId

        {

            get

            {

              return HttpContext.Current.Session["User"] == null ?

                  null :HttpContext.Current.Session["User"].ToString();

            }

            set

            {

                HttpContext.Current.Session["User"] = value;

            }

        }

 

        public int ItemInCart

        {

            get

            {

                return  HttpContext.Current.Session["Items"]== null ?

                    0 :Convert.ToInt32( HttpContext.Current.Session["Items"]);

            }

            set

            {

                HttpContext.Current.Session["Items"] = value;

            }

        }

        public Object SessionMain

        {

            get

            {

                return HttpContext.Current.Session["MasterUser"] == null ?

                    null : HttpContext.Current.Session["MasterUser"];

            }

            set

            {

                HttpContext.Current.Session["MasterUser"] = value;

            }

        }

 

    }

}

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.

public partial class Client : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        CentralSession Obj = new CentralSession();

        Obj.UserId = "123";

        Obj.ItemInCart = 1;

        Obj.SessionMain = Obj;

    }

}

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