Shopping cart session state done right in ASP.NET MVC

11,106

Steve Sanderson, in his book Pro ASP.NET MVC 2 Framework, gives a nice example of a how to implement a shopping cart using session in ASP.NET MVC. If you don´t have the book, you can get an idea reading here. It is a very neat approach. The idea is to create a model binder that takes the shopping cart from the session. The actions that use the shopping cart will get the cart "injected" by the model binder. When you´re testing those methods, your tests should be responsible for passing the shopping cart to the action.

Share:
11,106
Petrus Theron
Author by

Petrus Theron

I solve business problems with software. Over the past 15 years I have risked significant personal wealth to work on multiple startups. My unique experience allows me to understand a human desire, design a product that satisfies that need - then build and deploy it single-handedly from concept to scale. Sometimes I write about it. I primarily work in Clojure/ClojureScript, but I have built small to medium-sized products (sub-million LOC) using Python, C#, C/C++, Java, Pascal and JavaScript. Products I built: 2007-2011 Rhythm Music Store: online music store / record label that that sold 80k MP3s online. MyNames: an API to register .CO.ZA domains and provision nameservers. Stack: Python, AngularJS. Krit.com: a mobile customer feedback tool that used geolocation and SMS to bridge the gap between customers and retail managers 2007-2016: iFix (now weFix) Repair Management System tracks 500k repairs and millions in revenues at 36+ branches. Acquired by weFix. ...several others. There are more. Happy to delve into technical details. Good with people and recruiting.

Updated on June 14, 2022

Comments

  • Petrus Theron
    Petrus Theron over 1 year

    I'm implementing a simple session cart for unauthenticated users in ASP.NET MVC and I want to do it right.

    In the past I have always stored the cart ID in the persistent Session["CartID"] store and as a cookie. Whenever I need to display the cart, I'll look up the user's cart items from my Carts and CartItems tables. But inside I know a more strongly-typed approach would be cleaner.

    After Googling for the latest session MVC stuff, I found the term HttpSessionStateWrapper, which seems to be a testable way of dealing with sessions. But I have not found any good tutorials or standardised implementations. Maybe it's just a buzz-word and I should be sticking to Session["..."].

    What is the right way to implement a shopping cart using sessions in the latest version of ASP.NET MVC?

  • Petrus Theron
    Petrus Theron almost 13 years
    That's just a mocking implementation for the session state with "dirty" code like int pageSize = Session["SpaceController!Index!PageSize"] == null ? 10 : Convert.ToInt32(Session["SpaceController!Index!PageSize"]); Is there a cleaner, strongly-typed way that's tried and tested?
  • John Farrell
    John Farrell almost 13 years
    How you mock is up to you. How you strongly type your session access is up to you. Session is reliant on string keys and stores objects. Your going to have to work with those at some point. I wanted to point out the major differences in Asp.net MVC.
  • Petrus Theron
    Petrus Theron almost 13 years
    Thanks, this is exactly what I was looking for.
  • tinmac
    tinmac over 9 years
    @uvita I like this idea but isn't storing the whole cart in session a bad idea? I've deliberately avoided doing this in the past as we are told to keep session small, but if its getting the thumbs up im going for it. Great read too, thanks.