コード例 #1
0
        public bool OrderItem(string bookID, string userID)
        {
            bookstoreProxy.EnlistPurchase(bookID, 1);
            double price = bookstoreProxy.GetItemPrice(bookID);

            if (price == -1)
            {
                return(false);
            }

            bankProxy.EnlistMoneyTransfer(userID, price);

            tasks.Add(Task <bool> .Factory.StartNew(() => bookstoreProxy.Prepare()));
            tasks.Add(Task <bool> .Factory.StartNew(() => bankProxy.Prepare()));

            Task.WaitAll(tasks.ToArray());

            if (tasks[0].Result && tasks[1].Result)
            {
                tasks.Clear();
                tasks.Add(Task <bool> .Factory.StartNew(() => { bookstoreProxy.Commit(); return(true); }));
                tasks.Add(Task <bool> .Factory.StartNew(() => { bankProxy.Commit(); return(true); }));
                Task.WaitAll(tasks.ToArray());

                return(true);
            }
            else
            {
                tasks.Add(Task <bool> .Factory.StartNew(() => { bookstoreProxy.Rollback(); return(true); }));
                tasks.Add(Task <bool> .Factory.StartNew(() => { bankProxy.Rollback(); return(true); }));
                Task.WaitAll(tasks.ToArray());

                return(false);
            }
        }