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

  • SEARCH
  • Home
  • 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 8424287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:56:51+00:00 2026-06-10T03:56:51+00:00

I recently started with TDD and dependency injection. This is one BIG Mindset change,

  • 0

I recently started with TDD and dependency injection. This is one BIG Mindset change, and i am still trying to puzzle together all the pieces.

I have a service layer for all the business logic to keep the controllers as thin as possible.Some of the services in my service requires HttpContext. What would be the best way to create the service so that unit testing can be done as well.

Currently i have a ServiceBase that looks like this

public class HttpContextService 
{
    private HttpContextBase _ServiceContext;
    public HttpContextBase ServiceContext 
    { 
        get
        {
            if (_ServiceContext == null)
                _ServiceContext = new HttpContextWrapper(HttpContext.Current);
            return _ServiceContext;
        }  
        set { _ServiceContext = value; }  
    }
}

So this means that all my services can just inherit from this service. The reason i created a property for the HttpContextBase is so that I can set it, when unit testing the service. 1. Is there any way to rather inject the HttpContext into the service in Unity Bootstrap file. ?
2. Is this the right approach or am i missing something ?

thanks in advance. Like i said, i am struggeling to get my head around this whole TDD Dependency injection way of coding.

  • 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-06-10T03:56:52+00:00Added an answer on June 10, 2026 at 3:56 am

    Some of the services in my service requires HttpContext

    Your services should live in the business layer and this layer should have no notion of the presentation technology you use. This allows you to reuse this logic for different technologies (such as WCF web service, or Windows Service). But even if you don’t intend to reuse this code, tightly coupling your service to the HttpContextBase still increases complexity, and makes it harder to unit test, as you are already experiencing.

    The solution is to abstract the things you want from the HttpContextBase behind abstractions, and not an abstraction over the complete HttpContext, but over the individual functionality that you want to use in a certain case.

    For instance, when you want to access the HttpContextBase.User property, you’d be better of with the following abstraction:

    public interface IUserContext
    {
        IIdentity Current { get; }
    }
    

    This makes testing easier, your services simpler and makes creating different implementations for different applications simpler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently started using a custom ServiceHostFactory because I want to use dependency injection
I've recently started practising TDD and unit testing, with my main primers being the
I recently started working with a client that had an index.html file with this
Recently started programming Android Java (Eclipse), Im trying to make a simple reader app
Recently started working with the scrapy library. I am trying to scrape from a
I recently started getting this error while running gems or bundler. The only thing
I recently started playing around with writing Perl (v5.8.8) extensions using XS. One of
I recently started using WPF and the MVVM framework, one thing that I have
I recently started to use the Nodewords module in Drupal 6. This adds the
We recently started using maven for dependency management. Our team uses eclipse as it's

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.