views:

216

answers:

1

In this post the author recommends implementing a Wrapper for the session to ease testing and decoupling the controller code. I will like to obtain ideas on how to implement a good wrapper for this purpose.

I see that in CodeProject there is one sample but it looks way more than what I need.

EDIT 1:

Ok. Thanks to Joshua I have implement and simple session helper class. I am posting the class here to get your opinion and see if you will add something.

public interface ISessionHelper
{
    T Get<T>(string key);
}

public class HttpContextSessionHelper : ISessionHelper
{
    private readonly HttpContext _context;

    public HttpContextSessionHelper(HttpContext context)
    {
        _context = context;
    }

    public T Get<T>(string key)
    {
        object value = _context.Session[key];

        return value == null ? default(T) : (T)value;
    }
}

In the controller I have something like this:

    private readonly ISessionHelper _sessionHelper;

    public HomeController(ISessionHelper session)
    {
        _sessionHelper = session;
    }

    public HomeController()
    {
        _sessionHelper = new HttpContextSessionHelper(System.Web.HttpContext.Current);
    }
+1  A: 

One of the other answers to that question had a comment with this website that has a good simple wrapper example. The comment was made by murki.

Joshua