思路话语

。Arlen:思想有多远你就能走多远...

.net2.0 TransactionScope petshop

研究M$的petshop4.0代码时发现了TransactionScope。
它可以控制多个操作在同一个事务内完成。这对于把事务放在业务逻辑层的人来说确实很方便。
petshop代码:
BLL层下的OrderSynchronous.cs
using System.Transactions;
//
  public void Insert(PetShop.Model.OrderInfo order) {
                        
            
using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required)) {

                dal.Insert(order);

                
// 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();
            }

        }

using(TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
{
    //...
    ts.Complete();
}

posted on 2008-03-06 20:37  Arlen  阅读(330)  评论(0编辑  收藏  举报

导航