I wonder if there is a simple manner to handle some kind of transaction in C#. Let us say that we have two properties and we would like to update their values. We could write:
A = GetA();
B = GetB();
The problem is, that if the exception will be thrown while assigning B, then object will be in inconsistant state, because A has been updated. We can solve this by storing the current value of A and catching an exception when dealing with B:
var tempA = A;
A = GetA(); // A is up to date
try { B = GetB(); } catch { A = tempA; } // B is up to date also or A is reverted
Even above solution is not save because the exception can be thrown while reverting A, but the point is, if there are built in mechanisms in .NET that simplyfies such operations?
I could imagine a statement like the below:
transaction { A = GetA(); B = GetB(); }
Or code construction like:
Transaction.Enter();
A = GetA();
B = GetB();
Transaction.Leave();
Before transaction the machine state will be stored and after transaction it will be reverted in case of exception. Is there something like that in .NET?