How are you using session variables in Asp.net with C#.net

How are you using session variables in Asp.net with C#.net

  If you are loosing values stored inside session, below URL will tell you the possible scenarios where this can happen.

  http://www.revenmerchantservices.com/post/2010/04/19/session-loss-in-webgarden.aspx

kick it on DotNetKicks.com

    Many times I had seen developers are using session in asp.net like this.

on page load

if (session["User"] != null)

//  do soething

else
 
// do some
 


This becomes nightmare to debug or manage session objects as you have created session as per your requirement. Its nightmare to read the code.

For best practice follow below steps while working with Session objects.

Create a class, name it as XYZSessionManager. where prefix XYZ can be your comapny name or DB name.

e.g.

Lest create UserInfo entity which will hold FirstName, LastName, properties of User.

public class UserInfo
{
    private string _firstName;
    public string FirstName
    {
        get {return _firstName;}
        set {_firstName = value;}
    }
  
  private string _lastName;
   public string LastName
   {
     get{return _lastName;}
     set{_lastName = value;}
   }



Lets add this into session.

 public class XYZsessionManager
{

   public static UserInfo GetUserInfo

   {
 
       get
          {

              if( HttpContext.Current.Session["User"] != null)  // Check wheather session object exists or not              
               {

                     return (UserInfo)HttpContext.Current.Session["User"] ; // if exists unbox it into UserInfo object

                }

              else
              { 
                     UserInfo u  = New UserInfo();  // Instantiate
 
                     u = XYZController.Provider.GetUserInfo();
                 
                       // XyzController.provider.GetUserInfo will talke to your Db and it will populate the UserInfo entity
 
                     HttpContext.Current.Session["User"]     = u;

                       //  add UserInfo object u into Session.

                     return u; 
                       // don't cast the session into UserInfo and return. just return u . Avoid casting (Boxing / unboxing) .                     

             }

           }       
                 
   }


 }
 


ok we have done with the XYZ session manger. we can do similar steps for other session variable or Cache variables.

Now, How to access it into your code?

  Wrong way :

UserInfo u = new UserInfo();                         // No need to instantiate

u = XyzController.GetUserInfo();                     // this will request session

Correct way

UserInfo u = XyzController.GetUserInfo();      // without creating new instance of an object UserInfo, here we are just refering the refrence to allready created instance.

There is no need to create new copy of object who looks like UserInfo and refer it to another new instance. Are you getting my point?




Satalaj.

Currently rated 4.4 by 7 people

  • Currently 4.428571/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5