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.

4

1 回答 1

1

您是否考虑过使用 System.Transactions 中的 TransactionScope?这将使您的代码块具有事务性,并在引发异常时自动回滚更改。

如果您使用 CommittableTransaction,您可以获得对 Commit 和 Rollbacks 的更多控制,因此您可能需要考虑这一点。

于 2008-11-07T04:32:32.667 回答