I'm looking for thoughts on how to use Session in an ASP.NET MVC application? Especially when using master pages and trying to simply get data to the main page without bypassing the controller. This question began with me, asking many small questions, but then I managed to turn it into a solution that has not yet been implemented, but one that is somewhat doable. Thank you for your feedback.
My proposed solution is βwhat am I going to implement if someone doesn't say stop!β "
I have model classes inheriting from ModelBase - which contain the information needed on the main page (there is only one view on the page) for certain things that it displays in the mast or footer, as well as settings based on the configuration based on that who logged in.
My best solution is this: here is the product page:
Presumably: at some point, I was already stuck certain data in the session - for example, perhaps partnerId , which entered through the gateway page, or the currentLoggedInUserEmail property or a completely blown object.
I have a ModelBase class from which each model (e.g. ProductModel inherits
I have a class MySiteControllerBase (inherited from Controller) that is subclassed by ProductController .
In my action method in ProductController I create a model to represent the product using 'new ProductModel()' . This model class itself does not know anything about the session or how to populate the ModelBase . In fact, he does not even know about ModelBase - he simply inherits from it. My chain constructor does nothing (because I don't want to pass it a Session ).
I override View(...) in MySiteControllerBase for all overloads that accept a model parameter. I check if this parameter is of type ModelBase , and if I populate properties like partnerId and currentLoggedInUserEmail . Fortunately, because I'm inside a class that inherits from Controller , I have direct access to Session , so I can get them straight from there.
This method means that the properties on ModelBase automatically populated only by me, doing a 'return View(model)' . However, there is an obvious problem if for a model for ProductModel you need to access something specific on ModelBase . It is going to get zero because it is not full yet.
This problem can be solved by passing to Session in the new ProductModel(session) , which, in turn, will pass the constructor chain to the new ModelBase(session) . I really don't like this solution, although I like to think of the model as a rather dumb data structure that should not be aware of any external data constructs at all. Another solution might just be his wing, and if I ever find that the ProductController should consume everything that is defined in ModelBase , I just create the MySiteControllerBase.UpdateModelBase(productModel, session) method to explicitly populate it inside the ProductController . Hope this is clear!
Other questions that come to mind are:
- What about unit testing? Is there any abstraction around session state in MVC or should I create my own? I did a search in the source code for the session and nothing worked!
- How does session tracking work with / REST / FUL / URLS in MVC? Are there any cookie issues that I need to know about?
- Should I think of a session differently than I have traditionally had?