views:

186

answers:

2

I'm working on a web forms application which intends to be ported over to MVC for its vNext +1 release. But at the moment it's a .NET 3.5 SP1 web forms application.

I'm wanting to have our own context which provides some helpers on top of the standard HttpContext/ HttpRequest/ HttpResponse/ etc objects. Also, I'm wanting to have decoupling of the HttpContext classes from the context.

Because there is the intention to go MVC I thought it'd be a good idea to make our custom context work with the HttpContextBase (and associated classes) which shipped in the System.Web.Abstractions assembly.

I don't want to design a solution that solves some problems at the moment but needs to be re-written to achieve testability in MVC (and is just useless in MCV) but what I've achieved so far doesn't really seem that useful.

The problem is I can't find any good examples on how to achieve this, how to extend HttpContextWrapper, or HttpContextBase so that you can maintain seperation of concern.

A: 

Is it not enough to write some extension methods for the HttpContext? I've done this in my current application and it works well. I suppose it depends how complex you want you custom HttpContext class to be and whether it's intended to represent something fundamentally different than what the standard HttpContext is representing. I can't really think of a scenario for this though. Perhaps you can elaborate a little more on what your custom HttpContext will look like?

AdamRalph
It's not so much the actual context I want to change (although there are some additional properties), the bulk of what is being added is to request and response, I want different objects (wrapper objects). It also doesn't have a separation of concerns with extension methods
Slace
A: 

You should not need to override the HttpContextWrapper or HttpContextBase to achieve this but instead have your custom context work with a HttpContextBase object, this way you will be able to test your custom context in isolation.

public class MyContext
{

    // helper methods work with this context
    HttpContextBase _ctx;

    public MyContext(HttpContextBase context)
    {
     _ctx = context;
    }


}

And when you create the custom context in the actual application just initialise using

new MyContext(new HttpContextWrapper(HttpContext.Current));
Damien McGivern