Get/Set HttpContext Session Methods in BaseController vs Mocking HttpContextBase to create Get/Set methods

15,034

Solution 1

The second one seems the best. Although I would probably write those two as extension methods to the HttpSessionStateBase instead of putting them into a base controller. Like this:

public static class SessionExtensions
{
    public static T GetDataFromSession<T>(this HttpSessionStateBase session, string key)
    {
         return (T)session[key];
    }

    public static void SetDataInSession<T>(this HttpSessionStateBase session, string key, object value)
    {
         session[key] = value;
    }
}

and then inside the controllers, or helpers, or something that has an instance of HttpSessionStateBase use it:

public ActionResult Index()
{
    Session.SetDataInSession("key1", "value1");
    string value = Session.GetDataFromSession<string>("key1");
    ...
}

Writing session wrappers is useless in ASP.NET MVC as the HttpSessionStateBase provided by the framework is already an abstract class which could be easily mocked in unit tests.

Solution 2

Just a little correction for the SetDataInSession method of the latest post. In my opinion, it´s a elegant solution! Thanks Darin Dimitrov.

public static class SessionExtensions
{
 public static T GetDataFromSession<T>(this HttpSessionStateBase session, string key) {
            return (T)session[key];
        }

        public static void SetDataInSession(this HttpSessionStateBase session, string key, object value) {
            session[key] = value;
        }
}
  • First create this class, and after remember to refer its namespace in the Controller class that will call this methods.

  • When getting the session value:

string value = Session.GetDataFromSession<string>("key1");

The must be a compatible type with the object persisted in the session.

Share:
15,034
Mangesh
Author by

Mangesh

Make sure to vote up for good answers and motivate people.

Updated on June 04, 2022

Comments

  • Mangesh
    Mangesh almost 2 years

    I created Get/Set HttpContext Session Methods in BaseController class and also Mocked HttpContextBase and created Get/Set methods.

    Which is the best way to use it.

        HomeController : BaseController
        {
            var value1 = GetDataFromSession("key1") 
            SetDataInSession("key2",(object)"key2Value");
    
            Or
    
            var value2 = SessionWrapper.GetFromSession("key3");
            GetFromSession.SetDataInSession("key4",(object)"key4Value");
        }
    

       public class BaseController : Controller
       {
           public  T GetDataFromSession<T>(string key)
           {
              return (T) HttpContext.Session[key];
           }
    
           public void SetDataInSession(string key, object value)
           {
              HttpContext.Session[key] = value;
           }
       }
    

    Or

      public class BaseController : Controller
      {
         public ISessionWrapper SessionWrapper { get; set; }
    
         public BaseController()
         {
           SessionWrapper = new HttpContextSessionWrapper();
         }
      }
    
      public interface ISessionWrapper
      {
         T GetFromSession<T>(string key);
       void    SetInSession(string key, object value);
      }
    
      public class HttpContextSessionWrapper : ISessionWrapper
      {
         public  T GetFromSession<T>(string key)
         {
            return (T) HttpContext.Current.Session[key];
         }
    
         public void SetInSession(string key, object value)
         {
             HttpContext.Current.Session[key] = value;
         }
      }
    
  • DukeDidntNukeEm
    DukeDidntNukeEm over 7 years
    I fixed a typo via edit but then the all-wise rejected it. See if you can find it ;)