0

If I have an Address class that implements IEditableObject, I might have EndEdit implementation like this:

public void EndEdit()
{
    // BeginEdit would have set _editInProgress and save to *Editing fields
    if (_editInProgress)
    {
        _line1 = _line1Editing;
        _line2 = _line2Editing;
        _city = _cityEditing;
        _state = _stateEditing;
        _postalCode = _postalCodeEditing;
        _editInProgress = false;
    }
}

If there is an exception on _city, then _line1, _line2, and possibly _city should revert. This problem isn't limited to EndEdit but probably found in other places as well.

Jiho Han
  • 1,610
  • 1
  • 19
  • 41

1 Answers1

1

Did you consider using a TransactionScope from System.Transactions? This will make your code block transactional, and will automatically roll back a change if an exception is thrown.

You can gain more control of Commit and Rollbacks if you use CommittableTransaction, so you may want to consider that.

jons911
  • 1,836
  • 15
  • 19