PetShop4分析随手贴2

同步订单处理与库存更新
本文介绍了一个.NETPetShop应用程序中订单同步处理的实现细节,包括如何创建订单、调用信用卡处理程序、使用事务范围确保订单插入及库存更新的一致性。

F:/.NET Pet Shop 4.0/Web/CheckOut.aspx.cs

 

                    OrderInfo order = new OrderInfo(int.MinValue, DateTime.Now, User.Identity.Name, GetCreditCardInfo(), billingForm.Address, shippingForm.Address, Profile.ShoppingCart.Total, Profile.ShoppingCart.GetOrderLineItems(), null);

 

                    // insert

                    Order newOrder = new Order();

                    newOrder.Insert(order);

 

 

 

F:/.NET Pet Shop 4.0/BLL/Order.cs

 

        public void Insert(OrderInfo order) {

 

            // Call credit card procesor

            ProcessCreditCard(order);

 

            // Insert the order (a)synchrounously based on configuration

            orderInsertStrategy.Insert(order);

        }

 

 

F:/.NET Pet Shop 4.0/BLL/OrderSynchronous.cs

 

using System;

using System.Transactions;

using PetShop.IBLLStrategy;

 

namespace PetShop.BLL {

    /// <summary>

    /// This is a synchronous implementation of IOrderStrategy

    /// By implementing IOrderStrategy interface, the developer can add a new order insert strategy without re-compiling the whole BLL

    /// </summary>

    public class OrderSynchronous : IOrderStrategy {

 

        // Get an instance of the Order DAL using the DALFactory

        // Making this static will cache the DAL instance after the initial load

        private static readonly PetShop.IDAL.IOrder dal = PetShop.DALFactory.DataAccess.CreateOrder();

 

        /// <summary>

        /// Inserts the order and updates the inventory stock within a transaction.

        /// </summary>

        /// <param name="order">All information about the order</param>

        public void Insert(PetShop.Model.OrderInfo order) {

 

            using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required)) {

 

                dal.Insert(order);//order的同时把lineitem也插入进去

 

                // Update the inventory to reflect the current inventory after the order submission

                Inventory inventory = new Inventory();

                inventory.TakeStock(order.LineItems);//减少库存

 

                // Calling Complete commits the transaction.

                // Excluding this call by the end of TransactionScope's scope will rollback the transaction

                ts.Complete();

            }

        }

    }

}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值