MementoObserver

A memento observer, or monitor, is an instance of the MementoObserver class that observes changes occurring to a ChangeTrackingService instance.

var memento = new ChangeTrackingService();
var monitor = MementoObserver.Monitor(memento);

We can use a monitor to trigger, for example, the CanExecuteChanged event of a ICommand interface implementation:

var memento = new ChangeTrackingService();
var monitor = MementoObserver.Monitor(memento);

var saveCommand = DelegateCommand.Create()
    .OnCanExecute(state => memento.IsChanged)
    .OnExecute(state => /* execute the command */)
    .AddMonitor(monitor);

The above code is not very different from manually attaching the TrackingStateChanged event of the ChangeTrackingService instance and manually calling the RaiseCanExecuteChanged method of the DelegateCommand instance, it is simply more concise and easier to maintain.

Last updated