2012-12-30 18 views
5

Guardando il codice:Oggetti transazionali in C#?

class MyClass { public static int g=1;} 

using (TransactionScope tsTransScope = new TransactionScope()) 
{ 
    //Do stuff here 
    MyClass.g=999; 
    tsTransScope.Complete(); 
} 
  • Guardando il "fare roba qui" sezione: Che tipo di oggetti posso scrivere in là in modo che possano essere Transactionable? . So già che posso scrivere comandi ADO e che saranno rollback/commit quando necessario. Ma via C# POV: quanto implementazione dovrebbe una classe deve avere per poter essere Transactionable

  • Se si tratta di un TransactionScope come si chiama, all'interno di una clausola using (che è cercare (attuazione di alcune interfaccia o qualcosa?) + infine), la logica dice che se c'è un rollback: MyClass.g dovrebbe tornare il suo valore di 1. tuttavia. questo non sta succedendo. Quindi immagino sia relativo alla prima domanda. Come posso rendere MyClass Transactionable?

+0

Ci sono alcune interfacce da implementare per essere transazionali, è necessario fare qualcosa quando si esegue il rollback o il commento, vero? –

+0

@MSakherSawan I _assume_ c'è. Questa è la mia domanda in realtà. –

risposta

8

Si dovrebbe implementare System.Transactions.IEnlistmentNotification interfaccia, this articolo e this può aiutare a

Per transazionale di storage ready-made in memoria, non v'è (Software transactional memory), e STM.NET non è una roba di finale, ma Microsoft ci sta lavorando!

Un piccolo esempio:

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

} 

Consumare il codice:

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

* EDTI: AGGIUNTO G CUSTODE PER Royi :) *

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

} 

il codice sarà :

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

quindi in realtà quando si implementa questa interfaccia il valore di g sarà 1. cool –

+0

Si dovrebbe scrivere la logica di restituzione del valore al suo valore primario! Sto scrivendo un piccolo e buon esempio! –

+0

Grazie amico. buona risposta. –