学习PetShop3.0(4)购物车

终于到购物车了,在看这个之前应该已经明白了第三篇的那个模型,这样购物车基本也就明白了。
来看一下ShoppingCart.aspx这个页。
当你看好了一个宠物,比如可爱的Golden Retriever,嘿嘿,那就点add to cart按钮,这时就会跳到ShoppingCart.aspx,url里带了这个宠物的id号,根据该id号程序将该宠物放到cart里面。然后你可以再去挑别的宠物,比如一只猫(……),虽然这不是什么好主意。然后该宠物的id号又会被传到ShoppingCart.aspx,并添加到cart里面。在ShoppingCart.aspx里,你可以更改想要领养的宠物的数量,然后程序会根据你要求的数量来计算所需的钱以及该宠物是否还有剩余。在你做出决定后可以点proceed to checkout进入定单生成的环节。
上面是大体的流程。下面来看.net petshop是怎么实现这个cart的
基本的实现主要是BLL里的Cart和Model里的CartItemInfo,而Web.ProcessFlow的CartControler则负责具体的实现。想一想第三篇里的那个模型,具体到这里,每挑选一个宠物,就有一个CartItemInfo通过CartControler添加到了保存在Session里的Cart里面,最后生成定单的时候就从Session里把Cart的值取出来(CartControler有生成定单的方法,下一篇再说)。
来看一下ShoppingCart.aspx.cs里向Cart添加CartItemInfo的代码
// Create an instance of the cart controller
ProcessFlow.CartController cartController = new ProcessFlow.CartController();

myCart = cartController.GetCart(true);

if (!Page.IsPostBack){

// Get the itemdId from the query string
string itemId = Request["itemId"];

if (itemId != null){
// Clean the input string
itemId = WebComponents.CleanString.InputText(itemId, 50);
myCart.Add(itemId);
cartController.StoreCart(myCart);

}
}

先看这一句 myCart = cartController.GetCart(true);
GetCart方法用来生成一个Cart,它是先在Session里检查,如Session里没有保存Cart,就生成一个新的,否则就把保存在Session里的Cart取出来,然后使用Add方法把新的宠物加到Cart里。
public Cart GetCart(bool create){

// Fetch the cart object from session state
Cart myCart = (Cart)HttpContext.Current.Session[CART_KEY];

if ( myCart == null ){
if (create){
myCart = new Cart();
}else{ 
HttpContext.Current.Server.Transfer(URL_NOCART);
return null;
}
}

return myCart;
}

下面是Add方法的实现
public void Add(string ItemId) {
// _items是在Cart里保存的宠物集合,通过遍历来判断这是否是一个新类别
foreach (CartItemInfo cartItem in _items) {
if (ItemId == cartItem.ItemId) {
cartItem.Quantity++;
cartItem.InStock = (GetInStock(ItemId) - cartItem.Quantity) >= 0 ? true : false;
_total = _total+(cartItem.Price*cartItem.Quantity);
return;
}
}

//是一个新类别,则把一个CartItemInfo加入Cart,可以在这里看到CartItemInfo的组成
Item item = new Item();

ItemInfo data = item.GetItem(ItemId);
CartItemInfo newItem = new CartItemInfo(ItemId,data.Name, (data.Quantity >= 1), 1, (decimal)data.Price); 
_items.Add(newItem);
_total = _total+(data.Price);
}

这就完成了添加。然后是数量的更改。
// Check for update button
if (e.CommandName == CMD_UPDATE){
TextBox txt;
int qty;
int index;

// Go through each item on the page
for (int i = 0, j = cart.Items.Count; i < j; i++){

// lookup the control
txt = (TextBox)cart.Items[i].FindControl(ID_TXT);

try{
qty = int.Parse(txt.Text);
index = cart.CurrentPageIndex * cart.PageSize + i;

// If the new qty is zero, remove the item from the cart
if (qty <= 0)
myCart.RemoveAt(index); 
// Update the item with the new quantity
else
myCart[index].Quantity = qty;
}
catch {}
}

}else
// otherwise the command is to remove the an item
myCart.Remove((string)e.CommandArgument);

因为宠物的添加是先于数量的更改进行的,所以到了这里Session里肯定有保存Cart。数量的更改直接同过索引器来完成,更改后直接就会保存。Remove和RemoveAt都实现了从Cart删除指定的CartItemInfo。

ShoppingCart.aspx页还有其他的东西,比如一个继承自SimplePager的自定义控件,,还有判断是否显示用户喜好的宠物列表的判断。
Refresh方法用来重新计算total的值,哈,不过我不清楚微软究竟想拿这个值显示什么?在我下的这个版本里,根本就不是subtotla的总和,而是price的总和,但问题在于,当你把一种宠物从Cart里移除的时候,它竟然会total=total-subtotal,因此常常会出现负数……

购物车从开始到最后销毁,都是在和Session打交道,没有任何与数据库的交互。我不是很了解面向对象技术,但我觉得oo在这里得到了很好的体现。
posted @ 2005-11-03 16:04  torome  阅读(328)  评论(0编辑  收藏  举报