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

… absolutely nothing?

What part of the puzzle does it fill for ASP.NET WebForms and ASP.NET MVC respectively?

Can you somehow create a ASP.NET * base-application which uses System.Web.Abstractions so it can be used in both kinds of ASP.NET-web applications?

In that case, how did they retro-fit the classes in System.Web.Abstractions back into ASP.NET WebForms?

You can’t new up objects from the namespace, so it can’t be used for mocking, can it?

Update: Sorry for not being clear on that I know the problem with testing of HttpContext and other build-it ASP.NET-objects. But thanks for good explanations anyway.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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);


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x