Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5936443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:23:57+00:00 2026-05-22T15:23:57+00:00

In an effort to move to TDD and unit testable code I have read

  • 0

In an effort to move to TDD and unit testable code I have read that I should be using an HttpContext wrapper. In my service layer as well as in my controllers I have to access the HttpContext Session for some data I have stored there.

Can someone provide an example of an HttpContext Wrapper implementation for MVC 3

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:23:58+00:00Added an answer on May 22, 2026 at 3:23 pm

    The MVC Runtime already provides a HttpContextWrapper. What you need to implement is a wrapper around Session state, and encapsulate the fact that state is being accessed through HttpContext so that you can use DI or a Mocking framework to create a non-HttpContext backed SessionWrapper for your tests. Brad Wilson provides some good information on how to do this. However, if you don’t want to wade through the video (which contains advanced topics) here is the gist for wrapping Session:

    Create an interface representing strongly typed object you could typically access through Session:

    public interface ISessionWrapper
    {
        public UserPreferences CurrentUserPreferences{get;set;}
        ...
    }
    

    Create an implementation of the interface that uses Session as a backing store:

    public class HttpContextSessionWrapper : ISessionWrapper
    {
        private T GetFromSession<T>(string key)
        {
            return (T) HttpContext.Current.Session[key];
        }
    
        private void SetInSession(string key, object value)
        {
            HttpContext.Current.Session[key] = value;
        }
    
        public UserPreferences CurrentUserPreferences
        {
            get { return GetFromSession<UserPreferences>("CurrentUserPreferences"); }
            set { SetInSession("CurrentUserPreferences", value); }
        }
    
        ...
    }
    

    Resolve the instance in your Controller using DependencyResolver (or preferably this is done through you DI framework of choice). Assuming you are using the SessionWrapper in a majority of Controllers, this could be done in a common BaseController:

    var SessionWrapper = DependencyResolver.Current.GetService<ISessionWrapper>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In an effort to move to HTML5, does one have to abandon jQuery or
I've been refactoring my models and controllers in an effort to remove code duplication,
For obvious productivity reasons, I make an effort of learning and using as many
My company is fairly new to unit testing our code. I've been reading about
I have a bit old project that I would call legacy. Some characteristics of
In a seemingly never ending effort to learn more about iphone development, I have
I have been using WinForms since the first framework introduced and invested a lot
We have a licencing server which generates keys using the Java TrueLicense library. I
I've been making a concerted effort to improve my javascript skills lately by reading
I am making a strong effort to discipline my projects and create a Vision/Scope

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.