学习PetShop3.0(5)生成定单

点proceed to checkout后,就进入Checkout.aspx,确认后进入OrderBilling.aspx,在这里可以修改你的信息,完成后点continue,会出现个人信息的只读页面,最终确认后就进入OrderProcess.aspx,在这里是定单的详细情况,并且是只读的,到这里,定单被添加到数据库,购物完成。
Checkout.aspx把数据从Session中取出来,然后显示到页面,没什么好说的。
OrderBilling.aspx,这个页面一开始显示的信息可写,我们看OnLoad事件中,是用ProcessFlow.AccountController.GetAccountInfo获得用户的信息CreditCardInfo,然后显示在一些可写的控件中,比如用户控件:StaticAddress。当点击确认后,使用StoreCreditCard把刚才获得的用户信息保存到Sessin,准备呆会用。
OrderProcess.aspx是最终的定单生成页面。主要就是一个方法:
ProcessFlow.CartController.PurchaseCart 来看它的实现
public OrderInfo PurchaseCart(){

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

// Make some checks on the cart
if ( ( myCart == null ) || ( myCart.Count==0 ) ) {

HttpContext.Current.Server.Transfer(URL_NOCART);
//HttpContext.Current.Response.Redirect(URL_NOCART, false);
return null;

}else{

// Build up the order
OrderInfo newOrder = new OrderInfo();
newOrder.UserId = ((AccountInfo)HttpContext.Current.Session[ACCOUNT_KEY]).UserId;
newOrder.CreditCard = (CreditCardInfo)HttpContext.Current.Session[CREDITCARD_KEY];
newOrder.BillingAddress = (AddressInfo)HttpContext.Current.Session[BILLING_KEY];
newOrder.ShippingAddress = (AddressInfo)HttpContext.Current.Session[SHIPPING_KEY];

newOrder.LineItems = (LineItemInfo[])myCart.GetOrderLineItems().ToArray(typeof(LineItemInfo));

newOrder.OrderTotal = myCart.Total;
newOrder.Date = DateTime.Now;

// Send the order to the middle tier
OrderInsert order = new OrderInsert();
//向数据库插入数据
newOrder.OrderId = order.Insert(newOrder);

// clear the session objects used
HttpContext.Current.Session[CART_KEY] = null;
HttpContext.Current.Session[CREDITCARD_KEY] = null;
HttpContext.Current.Session[BILLING_KEY] = null;
HttpContext.Current.Session[SHIPPING_KEY] = null;

return newOrder;
}
}

Order主要是由保存在Session里的值形成,其中包括购物车。
在Order有一个LineItemInfo,它是由Cart.GetOrderLineItems方法依据cart里CartItemInfo的值返回的,是定单的物品部分,和CartItemInfo相比,主要是多了一个Line属性,这个Line是用来表示物品在定单内的序号。
下面是Insert的实现。
public int Insert(OrderInfo order) {

// Get an instance of the Order DAL using the DALFactory
IOrder dal = PetShop.DALFactory.Order.Create();

// Call the insert method in the DAL to insert the header
int orderId = dal.Insert(order);

// Get an instance of the Inventory business component
Inventory inventory = new Inventory();
//向数据库插入库存信息
inventory.TakeStock( order.LineItems);

// As part of the sample application we have created a user
// you can tested distributed transactions with
// If the order has been created with the user 'Acid',
// then throw an exception which will rollback the entire transaction

if (order.UserId == ACID_USER_ID)
throw new ApplicationException(ACID_ERROR_MSG);

// Set the orderId so that it can be returned to the caller
return orderId;
}

你可能感兴趣的:(学习PetShop3.0(5)生成定单)