Refactoring toward frictionless & odorless codeHiding global state
Originally posted at 3/30/2011
As I mentioned in the previous post, I don’t really like the code as written, so let us see what we can do to fix that. For a start, we have this code:
public class HomeController : Controller { public ActionResult Blog(int id) { var blog = MvcApplication.CurrentSession.Get<Blog>(id); return Json(blog, JsonRequestBehavior.AllowGet); } }
I don’t like the global reference in this manner, so let us see what we can do about it. The easiest way would be to just hide it very well:
public class HomeController : SessionController { public ActionResult Blog(int id) { var blog = Session.Get<Blog>(id); return Json(blog, JsonRequestBehavior.AllowGet); } } public class SessionController : Controller { public HttpSessionStateBase HttpSession { get { return base.Session; } } public new ISession Session { get { return MvcApplication.CurrentSession; } } }
So that result in nicer code, the architecture is pretty much the same, but we have a much nicer code for al of our controllers.
We are not done yet, though.
More posts in "Refactoring toward frictionless & odorless code" series:
- (12 Apr 2011) What about transactions?
- (11 Apr 2011) Getting rid of globals
- (10 Apr 2011) The case for the view model
- (09 Apr 2011) A broken home (controller)
- (08 Apr 2011) Limiting session scope
- (07 Apr 2011) Hiding global state
- (06 Apr 2011) The baseline

Comments
Comment preview