在C#中的交易 [英] Transactions in C#
问题描述
首先,这不会是一个关于数据库事务职位。我想知道更多关于 TransactionModel 在.NET 2.0及更高版本。由于我对.NET 3.5较新的型号apprechiated发展。
First of all, this will not be a post about Database Transactions. I want to know more about the TransactionModel in .NET 2.0 and higher. Since I am developing against .NET 3.5 newer models are apprechiated.
现在,我想acheive什么是一样的东西以下
Now, what I would like to acheive is something like the following
public void Withdraw(double amount)
{
using (TransactionScope scope = new TransactionScope())
{
Money -= amount;
if (Money > 0)
scope.Complete();
}
}
这将意味着,当这些钱是小于0 ,里面的一切的TransactionScope
应该是RolledBack,然而,事实并非如此。
Which would mean that when the Money is less than 0, everything inside the TransactionScope
should be RolledBack, however, it's not.
一个简单的测试,为后面
A simple test as followed
ImportantObject obj = new ImportantObject(1);
Console.WriteLine(obj.Money);
obj.Withdraw(101);
Console.WriteLine(obj.Money);
前提是Stadard金额为100。
Provided that the Stadard Money amount is 100.
难道我错过这里的东西,或者这是不是交易应如何工作的? ?什么都采用这种模式的性能损失。
Did I miss something here or is this not how the transactions should work? And what are the performance losses using this model?
推荐答案
您可能需要阅读的挥发性资源管理器中的.NET:将事务由Juval洛伊通用类型
这篇关于在C#中的交易的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!