.net2.0 TransactionScope petshop
研究M$的petshop4.0代码时发现了TransactionScope。
它可以控制多个操作在同一个事务内完成。这对于把事务放在业务逻辑层的人来说确实很方便。
petshop代码:
BLL层下的OrderSynchronous.cs。
using(TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
{
//...
ts.Complete();
}
它可以控制多个操作在同一个事务内完成。这对于把事务放在业务逻辑层的人来说确实很方便。
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();
}
}
//
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();
}