VBBRett
asked on
HttpContext Issue
I have some code that I copied from a shopping cart tutorial in C#, the one issue that I see with the code is that when you add things to the shopping cart for one user(user A) it also adds things to the shopping cart for another user(user B). How do I make a distinct update to a shopping cart for each individual user as they user the shopping cart? I have the code below that is attached.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ShoppingCart
{
public class ShoppingCart
{
#region Properties
public List<CartItem> Items { get; private set; }
#endregion
#region Singleton Implementation
//Readonly properties can only be set in initialization or in a constructor
public static readonly ShoppingCart Instance;
//The static constructor is called as soon as the class is loaded into memory
static ShoppingCart()
{
//If the cart is not in the session, create one and put it there
//Otherwise, get it from the session
if (HttpContext.Current.Session["ASPNETShoppingCart"] == null)
{
Instance = new ShoppingCart();
Instance.Items = new List<CartItem>();
HttpContext.Current.Session["ASPNETShoppingCart"] = Instance;
}
else
{
Instance = (ShoppingCart)HttpContext.Current.Session["ASPNETShoppingCart"];
}
}
//A protected constructor ensures that an object can't be created from outside
protected ShoppingCart() { }
#endregion
#region Item Modification Methods
/**
* AddItem() - Adds an item to the shopping
*/
public void AddItem(int productId)
{
//Create a new item to add to the shopping cart
CartItem newItem = new CartItem(productId);
//If this item already exists in our list of items, increase the quantity
//Otherwise, add the new item to the list
if (Items.Contains(newItem))
{
foreach (CartItem item in Items)
{
if (item.Equals(newItem))
{
item.Quantity++;
return;
}
}
}
else
{
newItem.Quantity = 1;
Items.Add(newItem);
}
}
public void SetItemQuantity(int productId, int quantity)
{
//If we are setting the quantity to 0, remove the item entirely
if (quantity == 0)
{
RemoveItem(productId);
return;
}
}
public void RemoveItem(int productId)
{
CartItem removeItem = new CartItem(productId);
Items.Remove(removeItem);
}
#endregion
#region Reporting Methods
/**GetSubTotal() - returns the total price of all the items
* before tax, shipping, etc.
*/
public decimal GetSubTotal()
{
decimal subTotal = 0;
foreach (CartItem item in Items)
subTotal += item.TotalPrice;
return subTotal;
}
#endregion
}
}
ASKER
Ok, besides getting rid of the statics, what else should I do? Can you show me example of code that would correct what I am doing?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This is kind of clumsy because, at the end of the day, you're still going to have multiple instances of the ShoppingCart class in memory... So I don't see the value of using the singleton pattern for this. To get rid of the confusion, I'd suggest removing the "static"'s and adjusting your code appropriately.