Standard approaches to implementing Undo/Redo

The Memento pattern is one of two standard ways of providing Undo/Redo functionality. It stores the state of the application or the part of the application that the Undo/Redo operations should apply to. The other approach is to use include an Undo() method in the Command interface while using the Command pattern. There are obvious benefits to each – one is simpler and less work to set up, and the other requires less memory.

If designing for undo/redo functionality, I would select the Memento pattern by default. I’d only look at the adapted form of the Command pattern if reasons such as memory consumption made the Memento approach unsuitable. Providing an Undo() method for every action available to the user can quickly become cumbersome, and the number and complexity of actions can expand quickly in applications. It’s best to start with the simplest approach possible and change later if additional complexity is required.

[Updated to correct spelling.]

UndoService

I recently published a generic undo/redo service on NuGet and GitHub. It is based on the momento pattern and uses delegate methods to access state.

The main benefit of this is how simple it is to use. To use it, you pass the type that is used to store state and the methods to get and set the state (most likely in wrappers to match the delegate signature). Once that is in place, you just invoke RecordState() to get the state and store it in the Undo stack, along with Undo(), Redo() and Clear() methods as appropriate.

The data structure it uses depends on whether you set a limit on the number of states that it stores. If a limit is set, it uses a dropout stack. Without a limit, it uses a standard stack.

There is no nesting or grouping of undoable actions. It is possible that I’ll add such a feature in the future but I have no specific plans to do so.

https://github.com/peterdongan/UndoService

https://www.nuget.org/packages/UndoService/

AutoScroller for UWP ScrollViewer

I recently published a couple of open source projects on NuGet and GitHub. One is UWP.AutoScroller. It auto-scrolls a UWP ScrollViewer when an element within it is dragged against its edge. It’s ported from a similar tool I wrote for Silverlight some time ago. 

I simplified its functionality. The Silverlight version also allowed you to programmatically scroll the ScrollViewer. This is not included in the new version – you just enable or disable the basic function.

I also simplified the code. In this version, handlers are added to events of the ScrollViewer itself instead of on elements within it. This is a simpler and more efficient approach.

Further information is in the project readme and the nuget documentation.

https://github.com/peterdongan/AutoScroller

https://www.nuget.org/packages/UWP.AutoScroller/