Transactions for C# objects?

前端 未结 8 806
青春惊慌失措
青春惊慌失措 2020-12-01 09:26

Just curious, is there any support for transactions on plain C# objects? Like

using (var transaction = new ObjectTransaction(obj))
{
  try
  {
    obj.Prop1          


        
8条回答
  •  死守一世寂寞
    2020-12-01 10:08

    Juval Lowy has written about this. Here is a link to his original MSDN article (I first heard about the idea in his excellent WCF book). Here's a code example from MSDN, which looks like what you want to achieve:

    public class MyClass
    {
       Transactional m_Number = new Transactional(3);
    
    
    public void MyMethod()
       {
          Transactional city = new Transactional("New York");
    
          using(TransactionScope scope = new TransactionScope())
          {
             city.Value = "London";
             m_Number.Value = 4;
             m_Number.Value++;
             Debug.Assert(m_Number.Value == 5);
    
             //No call to scope.Complete(), transaction will abort
          }
    }
    

提交回复
热议问题