Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Web.Abstractions: what is it good for?

The namespace contains types that are designed to wrap ASP.NET's core classes (such as HttpSessionState).

What, you want to know why?

Many of these core ASP.NET classes are sealed or static, making it impossible to break dependencies between them and your code. THAT means you can't mock these core dependencies, making it much harder to test your ASP.NET code. So, instead of directly manipulating the HttpResponse directly in code, you manipulate it via the HttpResponseWrapper, which, during test time, you can stub or mock out to control how the HttpResponse object behaves.

If you've ever thought about creating these wrappers, or have implemented one or two of them before, you'd know that there's lots of work wrapped up in that namespace, and I, for one, am glad they did it.


It is indeed for mocking. - HttpContext is a sealed class and cannot be (easily) mocked. HttpContextBase is not sealed and I believe it's methods are virtual, making mocking much easier.

While you can't new-up an instance of HttpContextBase (say, for use in WebForms), you can get an instance via:

var ctx = new HttpContextWrapper(HttpContext.Current);