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