HttpContext Can Break Object Oriented Principles
A situation came up when I was coding.. I was debugging a certain class, and somehow some value was mysteriously changing, and when I did a search in the code, I could not figure out how it was changing. Somehow the Session value for "SalesCode" was changing yet there was no reference to it.
The culprit?? One of the functions in a business object in App_Code was using HttpContext.Current without having that explained and documented and also without having it as one of the parameters..
Thus, using HttpContext.Current can break OOP (Object Oriented Principles).
If you want to make a change to the HttpContext (Session, or what not), pass it!
Here is the code that was messing with my session:
public static void SetSalesCode(int newValue)
{
HttpContext.Current.Session["SalesCode"] = newValue;
}
I modified the code, so that it still achieves the same effect, but it is much more obvious what it
does and easier to track down
public static void SetSalesCode(int newValue, HttpContext htc)
{
htc.Session["SalesCode"] = newValue;
}
Related Reading:
Other Interesting Posts
-
Articles
- January 2011
- April 2010
- March 2010
- February 2010
- January 2010
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- February 2009
- December 2008
- November 2008
- October 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
-
Meta







