5

コードを見る:

class MyClass {  public static int g=1;}

using (TransactionScope tsTransScope = new TransactionScope())
{
    //Do stuff here
    MyClass.g=999;
    tsTransScope.Complete();
}
  • Do stuff here」セクションを見てください。どのタイプのオブジェクトをそこに記述して、トランザクション可能にすることができますか?. ADO コマンドを記述でき、必要に応じてロールバック/コミットされることは既に知っています。しかし、POV を介して: クラスがトランザクションC#可能になるために必要な実装 (インターフェイスまたは何かを実装する必要がありますか?)

  • それがTransactionScope 呼び出されたとおりである場合、using句内(これはtry + finallyです)、ロジックは、ロールバックがある場合:MyClass.g1の値を取得する必要があることを示しています。ただし。これは起こっていません。したがって、最初の質問に関連していると思います。MyClass をTransactionableにするにはどうすればよいですか?

4

1 に答える 1

8

System.Transactions.IEnlistmentNotificationインターフェイスを実装する必要があります。この記事とこれが役立つ場合があります

既成のトランザクション インメモリ ストレージには (ソフトウェア トランザクション メモリ) があり、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 キーパーを追加 :) *

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();
}
于 2012-12-30T09:22:46.377 に答える