Link to home
Start Free TrialLog in
Avatar of cvsayani
cvsayaniFlag for India

asked on

Handling session in common place in asp.net

Can we handle all session in a single static class like ?

public static class SessionManager
{
// Will it be a problem to assign a object like this?
public static Criteria SearchCriteria
{
   get{
      Criteria searchCriteria = HttpContext.Current.Session["SearchCriteria"] as Criteria;
    if(searchCriteria ==null)
     {
       searchCriteria =new Criteria();
     }
   return searchCriteria ;
   }
set{
      HttpContext.Current.Session["SearchCriteria"] =value;
     }
}
// Will it be a problem to assign a string like this?
public static string UserName
{
   get{
      string userName= HttpContext.Current.Session["UserName"] as string;
    if(string.IsNullOrEmpty(IsuserName))
     {
        userName=<<CurrentUser>>;
     }
    return userName;
   }
set{
      HttpContext.Current.Session["userName"] =value;
     }
}

}

is this a good idea ?
ASKER CERTIFIED SOLUTION
Avatar of ChetOS82
ChetOS82
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cvsayani

ASKER

Will this be thread safe ?
These methods are just wrappers around the Session variable.  Since there is no state contained in the class, there are no thread issues.
Thanks Sage.