views:

168

answers:

1

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.

+1  A: 

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