Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Transactional ViewModel

David Sungaila edited this page Sep 18, 2020 · 1 revision

Inherit from PresentationBase.TrxViewModel. Make sure to call AcceptChanges() once you initialized your ViewModel.

public class AwesomeViewModel : TrxViewModel
{
    private string _name;
  
    public string Name
    {
        get => _name;
        set => SetProperty(ref _name, value);
    }
}

Short-term transactions

You wish you make small and frequent changes to your ViewModel? E.g. your ViewModel is part of a data grid?

Then you might be interested in using BeginEdit() , EndEdit() and CancelEdit().

Call BeginEdit() before you edit properties and then call EndEdit() to finish. If you wish to rollback, just call CancelEdit() and your ViewModel will revert to the point when BeginEdit() was called.

Long-term transactions

You make large changes and want to revert to the last known accepted state? E.g. your ViewModel is a dialog and you need an undo feature?

Then take a look at AcceptChanges() and RejectChanges().

Calling AcceptChanges() will create a snapshot of the current ViewModel state. RejectChanges() will revert the state to the very last snapshot.

Limitations

  • There is only one snapshot at any time (no multiple undo steps)
  • Properties to be reverted must be public (both getter and setter)
  • Reference types are not cloned (the reference is copied though)
  • Child ViewModels can be deep cloned if they inherit from TrxViewModel
  • Collections of TrxViewModel must use ObservableViewModelCollection<> (or a derived type)