Roy*_*mir 5 .net c# ado.net transactionscope
看代码:
class MyClass { public static int g=1;}
using (TransactionScope tsTransScope = new TransactionScope())
{
//Do stuff here
MyClass.g=999;
tsTransScope.Complete();
}
Run Code Online (Sandbox Code Playgroud)
看看" Do stuff here "部分:我可以在那里写什么类型的对象,这样它们可以成为可交易的?我已经知道我可以编写ADO命令,并且在需要时它们将被回滚/提交.但是通过C#POV:一个类必须具有什么实现才能成为可交易的(实现一些接口或什么?)
如果它是一个TransactionScope 正如它所调用的那样,在一个using子句中(最后是try +),逻辑说如果有回滚:MyClass.g应该返回其值为1的值.这不会发生.所以我想这与第一个问题有关.如何使MyClass成为可交易的?
你应该实现System.Transactions.IEnlistmentNotification的接口,这个文章,这可以帮助你
对于现成的事务性内存存储,有(软件事务内存)和STM.NET它不是最终的东西,但微软正在努力!
一个小例子:
using System.IO;
using System.Text;
using System.Transactions;
namespace Sakher.Transactions
{
public class TsansactionalFileWriter
{
private FileTransactionEnlistment fileTransactionEnlistment = new FileTransactionEnlistment();
public TsansactionalFileWriter(string filePath)
{
fileTransactionEnlistment.FilePath = filePath;
Transaction.Current.EnlistVolatile(fileTransactionEnlistment, EnlistmentOptions.None);
}
public void AppendText(string text)
{
fileTransactionEnlistment.Content.Append(text);
}
public void WriteAllText(string text)
{
fileTransactionEnlistment.Content = new StringBuilder(text);
}
}
public class FileTransactionEnlistment : IEnlistmentNotification
{
public string FilePath { get; set; }
public StringBuilder Content { get; set; }
public FileTransactionEnlistment()
{
Content = new StringBuilder();
}
public void Commit(Enlistment enlistment)
{
File.WriteAllText(FilePath, Content.ToString());
}
public void InDoubt(Enlistment enlistment)
{
}
public void Prepare(PreparingEnlistment preparingEnlistment)
{
//You can create the file here
preparingEnlistment.Prepared();
}
public void Rollback(Enlistment enlistment)
{
//Do ssomething when the transaction is rolled-back (You may delete the file if you have created it!)
}
}
}
Run Code Online (Sandbox Code Playgroud)
使用代码:
using (TransactionScope tr = new TransactionScope())
{
TsansactionalFileWriter writer = new TsansactionalFileWriter("c:\\myFile.txt");
writer.AppendText("sdfgssdfgsdf");
tr.Complete();
}
Run Code Online (Sandbox Code Playgroud)
*EDTI:为ROYI添加G保持者:)*
using System.Transactions;
namespace Sakher.Transactions
{
public class Royi_s_gReturnerClass
{
private GReturnerEnlistment fileTransactionEnlistment = new GReturnerEnlistment();
public Royi_s_gReturnerClass()
{
Transaction.Current.EnlistVolatile(fileTransactionEnlistment, EnlistmentOptions.None);
}
}
public class GReturnerEnlistment : IEnlistmentNotification
{
public int GOldValue { get; set; }
public GReturnerEnlistment()
{
GOldValue = MyClass.g;
}
public void Commit(Enlistment enlistment)
{
}
public void InDoubt(Enlistment enlistment)
{
}
public void Prepare(PreparingEnlistment preparingEnlistment)
{
preparingEnlistment.Prepared();
}
public void Rollback(Enlistment enlistment)
{
MyClass.g = GOldValue;
}
}
}
Run Code Online (Sandbox Code Playgroud)
您的代码将是:
class MyClass { public static int g=1;}
using (TransactionScope tsTransScope = new TransactionScope())
{
Royi_s_gReturnerClass returner = new Royi_s_gReturnerClass();
//Do stuff here
MyClass.g=999;
tsTransScope.Complete();
}
Run Code Online (Sandbox Code Playgroud)