c# - classic asp.net shoppingcart session state -


i have used example create shopping cart : http://net.tutsplus.com/tutorials/other/build-a-shopping-cart-in-aspnet/

its example, stores shoppingcart in session["cart"] state , should work fine.

but doesn't. event if close browser, or try different browsers, still maintains state?!?!

here constructor + add cart method:

public list<cartitem> items { get; private set; }          // readonly properties can set in initialization or in constructor         public static readonly shoppingcart instance;         // static constructor called class loaded memory         static shoppingcart()         {             // if cart not in session, create 1 , put there             // otherwise, session             if (httpcontext.current.session["mpbookscart"] == null)             {                 instance = new shoppingcart();                 instance.items = new list<cartitem>();                 httpcontext.current.session["mpbookscart"] = instance;             }             else             {                 instance = (shoppingcart)httpcontext.current.session["mpbookscart"];             }         }         // protected constructor ensures object can't created outside         protected shoppingcart() { }          public void additem(int book_id)         {             // create new item add cart             cartitem newitem = new cartitem(book_id);             // if item exists in our list of items, increase quantity             // otherwise, add new item list             if (this.items.contains(newitem))             {                 foreach (cartitem in items)                 {                     if (i.equals(newitem))                     {                         i.quantity++;                         return;                     }                 }             }             else             {                 newitem.quantity = 1;                 items.add(newitem);             }          } 

may please advise on issue might be?

i've read 2 hours regarding session state , everywhere says should volatile when closing broser, in case isn't.

regards, alex

i not sure using singleton pattern hold instance of session. if think session need unique each user , each browser accesses website. singleton pattern creates single global unique instance. don't know how asp.net have done but, in case new asp.net session unique specific browser instance. means each browser accessing session["mpbookscart"] access own unique copy of data. default asp.net session hold data 20 minutes (this can configured in database). if writing shopping cart probrably work directly cart table , cartitems table in database. example of storefront website rob connery's mvc samples app. asp.net mvc application if aren't familiar mvc may find little bit hard follow.


Comments