title: How should I integrate session state into an ASP.NET MVC application?
body: <p>I'm looking for thoughts on how should I use Session in an ASP.NET MVC application? Especially when using masterpages and tryin to just get the data to the masterpage without bypassing the controller. This question started off by me asking a lot of little questions, but then I managed to mould it into a solution which as yet I have not implemented but one which is somewhat workable. Would appreciate any feedback.</p>
  <hr>
  <p><strong>My proposed solution aka 'what i am about to implement unless someone says stop!'</strong></p>
  <p>I have my model classes inheriting from ModelBase -- which contains the information needed by the masterpage (there is only one view per page) for certain things it displays in the masthead or footer as well as configuration driven settings based upon who is logged in.</p>
  <p>My best solution is as follows - shown here for a 'products page':</p>
  <blockquote>
  <p><em>Assumption</em>: I have at some point already stuck certain data in session - for instance perhaps a <code>partnerId</code> which came in through a gateway page, or a <code>currentLoggedInUserEmail</code> property or a fully blown object.</p>
  <p>I have a <code>ModelBase</code> class from which every model - such as <code>ProductModel</code> inherits</p>
  <p>I have a <code>MySiteControllerBase</code> class (inherits from Controller) - which is subclassed by <code>ProductController</code>.</p>
  <p>In my action method in <code>ProductController</code> I create the model for the product view with <code>'new ProductModel()'</code>. This model class itself knows nothing about session or how to populate <code>ModelBase</code>. It essentially doesn't even know about <code>ModelBase</code> - it just inherits from it. My chained constructor does nothing (because I don't want to pass it <code>Session</code>).</p>
  <p>I override <code>View(...)</code> in <code>MySiteControllerBase</code> for all the overloads that take a model parameter. I check to see if that parameter is of type <code>ModelBase</code> and if it is I populate the properties such as <code>partnerid</code> and <code>currentLoggedInuserEmail</code>. Fortunately because I'm inside a class that inherits from <code>Controller</code> I have direct access to <code>Session</code> so i can pull them straight out of there.</p>
  </blockquote>
  <p>This method means that the properties on <code>ModelBase</code> are automatically populated just by me doing <code>'return View(model)'</code>. However there is an obvious issue if the model for <code>ProductModel</code> needs to access anything defined on <code>ModelBase</code>. It's going to get null because it isn't populated yet.</p>
  <p>This issue can be solved by passing in <code>Session</code> to <code>new ProductModel(session)</code> which would in turn pass it up the constructor chain to <code>new ModelBase(session)</code>. I really <em>dont</em> like that solution though becasue I like to think of a model as a pretty dumb data structure that shouldn't know about any external data constructs at all. Another solution might be to just wing it, and if i ever find that <code>ProductController</code> needs to consume anything defined in <code>ModelBase</code> that I just create a method <code>MySiteControllerBase.UpdateModelBase(productModel, session)</code> to explicitly populate it inside <code>ProductController</code>. I hope thats clear!</p>
  <p>Other questions that come to mind are :</p>
  <ul>
  <li>What about unit testing? Is there any abstraction around Session state in MVC or should I build my own? I did a search in the sourcecode for 'session' and nothing came up!</li>
  <li>How does session tracking work with /REST/FUL/URLS in MVC? Are there any issues ith cookies off that I need to know about?</li>
  <li>Should I think of session differently from how I traditionally have?</li>
  </ul>