c# - C# 中的可事务对象?

标签 c# .net ado.net transactionscope

查看代码:

class MyClass {  public static int g=1;}

using (TransactionScope tsTransScope = new TransactionScope())
{
    //Do stuff here
    MyClass.g=999;
    tsTransScope.Complete();
}
  • 查看“在这里做事”部分:我可以在其中写入哪些类型对象,以便它们可以可交易 ?我已经知道我可以编写 ADO 命令,它们将在必要时回滚/提交。但是通过 C# POV:类必须具有什么实现才能Transactionable(实现某些接口(interface)或其他东西?)

  • 如果它是它调用的 TransactionScope,在 using 子句(即 try + finally)中,逻辑表明如果有回滚: MyClass.g 应该取回其值 1。但是。这没有发生。 所以我想这与第一个问题有关。我怎样才能使 MyClass Transactionable

最佳答案

你应该实现 System.Transactions.IEnlistmentNotification界面,this文章和this可能对你有帮助

对于现成的事务性内存存储,有 ( Software transactional memory ) 和 STM.NET这不是最终的东西,但 Microsoft 正在努力!

一个小例子:

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!)
        }
    }

}

使用代码:

        using (TransactionScope tr = new TransactionScope())
        {
            TsansactionalFileWriter writer = new TsansactionalFileWriter("c:\\myFile.txt");
            writer.AppendText("sdfgssdfgsdf");
            tr.Complete();
        }

* EDTI:为 ROYI 添加了 G KEEPER :) *

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

}

您的代码将是:

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

关于c# - C# 中的可事务对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14089592/

相关文章:

c# - 如何在不使用对话框的情况下在 C# 项目中为 MySQL 数据库设置连接字符串?

winforms - 我们如何解决所有这些 "Conversion from type DBNull to type String is not valid"的问题?

c# - 在 ADO.NET 中合并对两个数据库的查询

c# - 在 ASP.NET Core 应用程序中从 wwwroot 提供 SPA 时如何控制浏览器缓存过期

c# - 我是一个初学者,我无法解决C#代码中的一些错误

c# - TFS 2010 - 为什么我在模拟时收到 "TF30063 You are not authorized to access.."错误?

.net - 具有非实体返回类型的实体模型中的函数导入

.net - ADO.NET 是在 .net 中访问数据库的唯一本地方式吗?

c# - 如何在 C# 的 SqlDataAdapter 中使用 WHERE

c# - 具有复杂相等性的 HashSet