IContext = Good

Andrew Stopford asks: 

One of the comments in Phil's post on using the MVP patten is mapping the Context object to a type that is based on a IContext pattern. For an example of what this does take a look at the interface here and its creation here (CreateContext method). It feels wrong me to though as your adding the request and response data to a collection and getting at the data that way and it seems to go from one object layout to another. I guess that for unit test purposes (mocking the data that this contains for example) this would be useful, am I right in my thinking here. [1]

I can say confidently that context objects are definately a good thing. Now, that isn't to say that every app needs them, but most complex apps can benefit from them. Testing your classes inside something like NUnit is only one part of the story. There are other instances where you might want to take part of the app outside of the web. For instance, while working on Articulate Online (very sweet LMSish product we are about to knock the socks off the entire e-Learning industry with), we decided we wanted to run reports in an external service rather than tying up the web processes. All of the sudden we were faced with having to be able to make calls into any of our classes (which make use of context to access things stored in places like session or query strings while on the web). Had we not made sure to create a context object to handle accessing this stuff (lessons learned from building a CMS product ealier), we would have been forced to go through our code and weed out all the Session["..."], Request["..."], and HttpContext references. Fortunately, planning ahead made running our code outside the web a seamless process.

[1] http://weblogs.asp.net/astopford/archive/2006/08/23/IContext_2C00_-good-or-bad.aspx


1 Comment

  • Do I have this right?

    You would create your IContext object by passing in the HttpContext. Once you have your IContext, you would pass this to the Presenter so that the presenter could access it?

    Is the primary purpose of doing this is so you can run tests against it, and provide a mock object for IContext, so you are not really relying on the HttpContext object?

Comments have been disabled for this content.