Click here to Skip to main content
15,867,330 members
Articles / Programming Languages / C#

Automated Undo/Redo library based on C# Generics in .NET framework

Rate me:
Please Sign up or sign in to vote.
4.94/5 (186 votes)
21 Mar 2012CPOL13 min read 302.7K   2.8K   476   90
A reusable library that can equip any action in your application with the undo/redo feature.
Screenshot - article.png

Introduction

This article introduces a reusable library that can equip any action in your application with the undo/redo feature. You may use complex data structures and complex algorithms with no care about how they will be rolled back on user demand or after an error.

Background

If you've ever developed a graphic editor or designer for complex data, you've encountered the daunting task of implementing an undo/redo feature supported throughout the application. Implementing the mirror methods Do and Undo for each operation is a boring and very bug-prone process when you are building something more exquisite than a calculator. After my experiments then, I researched a way to make undo/redo support transparent for business logic. To achieve it, we will use the magic of generics.

This project has been published on CodePlex so that everybody can contribute to it and gain from it.

Using the Code

There are two points of good news. First, the public properties of your data classes will not have to be changed. We will just declare the private fields in another way than usual. Second, the business logic of processing your data objects will not be changed either. All we have to do is embrace this code with start and end points like a transaction. So, the code will look like the following:

C#
UndoRedoManager.Start("My Command"); // start point

myData1.Name = "Name1";
myData2.Weight = 33;
myData3.MyList.Add(myData2);

UndoRedoManager.Commit(); // end point

All of the changes made in the code block above can be rolled back with one line:

C#
UndoRedoManager.Undo();

The next line allows enforcing of the changes again:

C#
UndoRedoManager.Redo();

Let me draw your attention to the fact that no matter how many objects were involved in an operation and what data types were used, all changes can be undone/redone as a single transaction. Both value and reference data types work well. UndoRedoFramework also supports lists and dictionaries. Now let's consider how to declare a data class and make it work. Here is the trick: private data fields should be wrapped in the special generic type UndoRedo<>:

C#
class MyData
{
    private readonly UndoRedo<string> name = new UndoRedo<string />("");

    public string string Name
    {
        get { return name.Value; }
        set { name.Value = value; }
    }
//...
}

Below is a classic declaration of a property with a backing field, so you can compare it with the previous example.

C#
class MyData
{
    private string name = "";

    public string string Name
    {
        get { return name; }
        set { name = value; }
    }
//...
}

There are three vital differences in these code snippets:

  1. I use the UndoRedo<> generic type for backing a field. The field does not store values itself, but refers the container that stores the value for us.
  2. Whenever we need access to the backing field, we use name.Value in place of name. As you can see, name.Value is used without any type conversion. That is because Value will always have the type we nested into the UndoRedo<...> brackets.
  3. The private field is made readonly. This is because the field holds the container that is responsible for accumulating the history of changes. It must live as long as the parent instance. We do not have to change the container itself but, of course, we can change the Value.

This works well both for value and reference types.

Implementation

If you are not interested in implementation details, you may go to the next section with no hesitation. In this article, I will highlight only a couple of core implementation points. I hope you will cast a glimpse into the source code for further details. It is quite small and simple. There are two core classes in the framework: UndoRedoManager and UndoRedo<>. UndoRedoManager is a facade class that contains static methods for manipulations with commands. Below is a partial list of methods:

C#
public static class UndoRedoManager
{
    public static IDisposable Start(string commandCaption) { ... }

    public static void Commit() { ... }
    public static void Cancel() { ... }
    
    public static void Undo() { ... }
    public static void Redo() { ... }
    
    public static bool CanUndo { get { ... } }
    public static bool CanRedo { get { ... } }
    
    public static void FlushHistory() { ... }
}

Beyond UndoRedoManager, there is the following internal logical structure of objects:

  • \UndoRedoManager
  • \History
  • \Command
  • \Change
  • \OldValue,NewValue

In other words, UndoRedoManager stores the history of commands. Every Command has its own changes list. Every Change stores old and new values. The Change object is created by UndoRedo<> when the user attempts to make any modifications. As you may remember, we used the UndoRedo<> type to declare backing fields in the samples above. This class is responsible for creating a Change object and filling it with old and new values. Below is a key part of this class:

C#
public class UndoRedo<TValue> : IUndoRedoMember
{
    //...
    TValue tValue;
    
    public TValue Value
    {
        get { return tValue; }
        set 
        {
            if (!UndoRedoManager.CurrentCommand.ContainsKey(this))
            {
                Change<TValue> change = new Change<TValue>();
                change.OldState = tValue;
                UndoRedoManager.CurrentCommand[this] = change;
            }
            tValue = value;
        }
    }
    //...
}

The code above is the key point of the whole framework. It shows how changes are intercepted inside the user property that we declared in the previous chapter. Thanks to generics, the Value property eliminates any type conversion. The Change object is created inside the current command at only the first attempt of setting a property. So, we have the least amount of overhead possible. When the user commits the command, every Change object will be filled with a new value. The framework will automatically invoke the OnCommit method for each changed property:

C#
public class UndoRedo<TValue> : IUndoRedoMember
{
    //...
    void IUndoRedoMember.OnCommit(object change)
    {
        ((Change<TValue>)change).NewState = tValue;
    }
    //...
}

The old and new values gathered above are used by the framework to perform undo/redo operations. As you will see later in the Performance section, all of these routines give a very small overhead. In a real-world application, it can be less than 1%.

Collections

As I mentioned before, changes in lists and dictionaries can be undone/redone, as can those in simple properties. For these purposes, the library provides the classes UndoRedoList<> and UndoRedoDictionary<>, which have exactly the same interfaces as the standard List<> and Dictionary<> classes. However, despite this similarity, the internal implementation of these classes is upgraded with the undo/redo ability. Let's consider how a data object can declare a list:

C#
class MyData
{
    private readonly UndoRedoList<MyData> myList= new UndoRedoList<MyData>();

    public UndoRedoList<MyData> MyList
    {
        get { return myList; }
    }
}

The major caveat here is that the list is transactionable, but the reference to the list is not. In other words, we are free to Add, Remove and Sort items and all of these changes will be correctly undone. However, in the code above, we may not change the list's reference to another one because it is readonly.

Actually, my practice shows that this is not a problem because in most cases, a typical list stored in a field lives as long as the parent data object. If you are used to another design and want to change the reference to the list, you should use a little bit more sophisticated code. Combine the two generics UndoRedo<> and UndoRedoList<>, as in the following example:

C#
private readonly UndoRedo<UndoRedoList<MyData>> myList ...

Dictionary can be used in the same way as List. So, I won't produce a tautology.

Fail-proof Code

Sometimes code may fail during execution. An I/O error or internal exception may lead to data inconsistency even when the exception was caught carefully. UndoRedoFramework can help here and bring all data into the consistent state it was in at the start point. If the code passes with no error, all changes will be committed. Otherwise, it will be automatically rolled back. See below:

C#
try 
{
    UndoRedoManager.Start("My Command");
    // some code throwing an exception may be here
    //...
    UndoRedoManager.Commit();
}
catch (Exception)
{
    UndoRedoManager.Cancel();
}

Moreover, it is possible to use a more slick approach with the same result:

C#
using (UndoRedoManager.Start("My Command"))
{
    // some code throwing an exception may be here    
    //... 
    UndoRedoManager.Commit(); 
}

As you can see, the latest sample does not have a rollback routine. That is because it will be done automatically (!) if Commit is not reached, e.g. in the case of an exception. It gains ultimate reliability even if you do not need the undo/redo feature itself. The application will recover after any failure and retain a consistent state.

UI and Data Synchronization

A complex UI is often implemented using the Model-View-Controller pattern. A simple Windows application has only Data and Presentation tiers. However, in both cases the developer must write some sort of synchronization code between UI and Data. The demo project has a main form that contains three UI controls:

Screenshot - article2.png
  1. EditCityControl
  2. CitiesChartControl
  3. UndoRedoControl, which displays two lists of commands that can be undone/redone for cities data

These controls show different views of the same cities data. The real-world application -- i.e. a designer or any editor -- has dozens of such components and controls to display. However, there is the problem of synchronization: If one of the controls changes the data, ALL of the controls have to reload their data. This is because changing one entity may cause a business rule that changes some other entities.

So, what do we need? First, the synchronization code must get to know about changes made in a component or somewhere in the business logic. Second, it has to force controls on the form to reload and show new data.

This problem can be solved in several ways, good and bad. I want a decoupled code. Ideally, the form should not know much about components. Similarly, the components should not know about each other. Business rules should be as least aware as possible. Honestly, I am too lazy to write synchronization code every time I add another new component to the form. So, cast a glance at the demo project attached to the article and you will see a very lean form:

C#
public partial class DemoForm : Form
{
    public DemoForm()
    {
        InitializeComponent();

        // init data
        CitiesList cities = CitiesList.Load();
        chartControl.SetData(cities);
        editCityControl.SetData(cities);
    }
}

The form simply loads initial data into the custom controls. In this way, the form does not do any synchronization. Event handlers of controls have no code synchronization either, e.g. EditCityControl has a handler of the "Remove City" button:

C#
private void removeCity_Click(object sender, EventArgs e)
{
    if (CurrentCity != null)
    {
        UndoRedoManager.Start("Remove " + CurrentCity.Name);
        cities.Remove(CurrentCity);
        UndoRedoManager.Commit();
    }
}

This notwithstanding, all of the controls on the form are refreshed immediately after the action. This is because UndoRedoFramework provides an ad hoc event, notifying when a command is done/undone/redone. It allows us to put all UI-refreshing code at a single place in the control:

C#
public EditCityControl()
{
    //...
    UndoRedoManager.CommandDone += delegate { ReloadData();  };
    //...
}

Thus, solely subscribing to the CommandDone event, the control solves a bunch of problems. It always stays up-to-date when data has been changed in some other component. Moreover, the control will be immediately updated with the correct data when the user chooses to undo/redo operations.

Performance

Performance and memory optimizations are always competing with each other... and probably with the developer's girlfriend for his spare time. In this article, I will consider the first two factors only. Fortunately, editors and designers with user interaction have no rigid performance requirements for most operations, in contrast to real-time systems. Anyway, I will bring a short performance analysis in some points below:

  • Reading the data does not cause any overhead. Thanks to generics, we have gotten rid of type conversions and boxing/unboxing. Values are returned immediately, without searching in history logs.
  • Any attempt to change a property incurs one search in an internal hash table. Additionally, the first attempt to change a property during operation copies its value into the history.
  • Any attempt to change a list incurs one search in an internal hash table. Additionally, the first attempt to change the list forces the Framework to create a copy of the list and put it into its history.
  • Any attempt to change the dictionary incurs one search in an internal hash table and creates two small objects. The Framework never creates an entire copy of the dictionary. In place of that, it stores changes in a log.
  • The Undo and Redo methods simply restore changed values. They do not touch unchanged ones. Restoring a list is a O(1) operation. Therefore Undo may work even faster than the primary algorithm itself.

In other words, you may do the following without considerable overhead:

  • Read and change any properties
  • Read and change lists with average size
  • Read and moderately change dictionaries with large size

However, if you change several huge lists often, there is some risk of slowdown. In this case, I would recommend revision of the data design with splitting long lists.

Below, I bring up a profiling sample from my real-world application. In this example, the user resized a graphical object inside a designer. This middle-sized operation did 5500 readings, 70 changes of properties and 4 changes of dictionaries. All of the extra stuff required for undo/redo took less than 0.7 milliseconds (1/7000 of second). Here are the details of how UndoRedoFramework brought influence on performance:

Hit countTotal millisecondsTotal %
Whole resizing logic and redrawing routines159.328100%
UndoRedo routines0.6770.425%
Starting a command10.0080.005%
Getting a property value54610.1140.071%
Setting a property value710.0260.017%
Changing a dictionary40.0650.041%
Finishing a command10.4630.291%

Memory

UndoRedoFramework needs memory for storing changes only. Memory consumption does not depend on overall data volume, but on how much data was changed. So, your history of changes will take kilobytes even if the overall data takes megabytes. The length of the history is not limited by default, but it can be constrained with the static property UndoRedoManager.MaxHistorySize. This property sets how many of the last operations are stored. The oldest operation will be thrown away from the history when the maximum size is exceeded.

Additionally, let me give some advanced details about references and garbage collection: In a field that stores a reference to an object, it can be substituted by another reference. In .NET, the substituted object is put into the garbage if no more references are. So, this object can be thrown away at any time by the garbage collector. This does not fit our plans because we want to roll back to the old state, including the reference and its object. Luckily, our history holds the old reference. So, the object will not be purged even if the original data model forsook it.

The bottom line is that the old objects are protected from GC until the corresponding operation is removed from the history. It guarantees data consistency, but you should take it into account when you think about consumed memory.

Generics vs. Proxies

There is an alternative method of undo/redo implementation using proxies. Proxies exploit the calling context feature of .NET to seamlessly intercept properties invocations. It allows changes to be logged and rolled back. You can find some articles about that on this site. These are good articles written by really smart people. Here, I would like to summarize the difference between the generic and proxy approaches.

Proxy suggests an "external" way of intercepting changes. It catches a setter call before it reaches the property. Undoing is an external operation, too. To restore the value, proxy invokes a property setter on its own wish. However, there are many side effects: What if the property changes other properties? What if it fires a notification event? What if it checks a business rule against another property that may not be restored yet? The bottom line is that if you have "rich" properties, undo/redo will provoke unpredictable code execution.

Generics come from the "internal" side. This technique intercepts and restores changes beyond the property and its logic. The undo process is completely invisible. Business rules and event notifications will not be repeated. Hereby, application code has no chance to run and see data in inconsistent states during recovery.

I hope you have gotten the difference.

Further Development

I have some advanced features currently under research and prototyping:

  • Implementing isolated "areas" where data changes are being accumulated independently. It will be useful in multi-document applications where the user can undo/redo each document separately.
  • Enlisting files into transactions. Sometimes your data processing may save and refer some intermediate files, like asset files or banal temporary files. In such cases, the versions of these files should be consistent with the data in memory, which can be rolled back. The Framework will be able to synchronously undo file changes, along with undoing data objects in memory.
  • Multi-threading support, with concern to the first two points.

This project has been published on CodePlex so that everybody can contribute to it and gain from it.

History

  • July 10, 2007 - Initial version posted
  • July 12, 2007 - "Generics vs. Proxies" section added
  • July 31, 2007 - Performance sample added

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralSystem.Transactions Pin
tag20019-Oct-07 5:56
tag20019-Oct-07 5:56 
GeneralRe: System.Transactions Pin
Sergey Arhipenko10-Oct-07 2:30
Sergey Arhipenko10-Oct-07 2:30 
GeneralActually this is similar to what I've done. Pin
htna3-Oct-07 16:14
htna3-Oct-07 16:14 
GeneralImproving memory usage suggestion [modified] Pin
Jcmorin3-Oct-07 3:50
Jcmorin3-Oct-07 3:50 
GeneralYou're quite obviously full of it... Pin
sherifffruitfly2-Oct-07 12:41
sherifffruitfly2-Oct-07 12:41 
GeneralRe: You're quite obviously full of it... Pin
Anthony Daly21-Jun-09 22:38
Anthony Daly21-Jun-09 22:38 
Questionmultiple undo/redo Pin
123456789asdfasdf20-Sep-07 21:40
123456789asdfasdf20-Sep-07 21:40 
AnswerRe: multiple undo/redo Pin
Jcmorin1-Oct-07 12:03
Jcmorin1-Oct-07 12:03 
I thinks the number of undo depend on the number of UndoRedoManager.Start(...)/Commit()
So if you do a commit after the 3 city added, you will have only 1 undo.


GeneralRe: multiple undo/redo Pin
123456789asdfasdf1-Oct-07 16:51
123456789asdfasdf1-Oct-07 16:51 
QuestionUse of own classes Pin
ChrisVaughan14-Aug-07 20:53
ChrisVaughan14-Aug-07 20:53 
Generaluse implicit conversion to make those properties more readable Pin
Jay R. Wren14-Aug-07 4:09
Jay R. Wren14-Aug-07 4:09 
GeneralChanging BusinessObject without UndoRedo [modified] Pin
okrumnow9-Aug-07 23:00
okrumnow9-Aug-07 23:00 
GeneralRe: Changing BusinessObject without UndoRedo Pin
Sergey Arhipenko12-Aug-07 20:43
Sergey Arhipenko12-Aug-07 20:43 
GeneralRe: Changing BusinessObject without UndoRedo Pin
Mystic Taz8-Oct-07 21:03
Mystic Taz8-Oct-07 21:03 
GeneralRe: Changing BusinessObject without UndoRedo Pin
Sergey Arhipenko8-Oct-07 22:16
Sergey Arhipenko8-Oct-07 22:16 
GeneralExcellent article Pin
elibriscoe8-Aug-07 12:58
elibriscoe8-Aug-07 12:58 
GeneralLooking forward to updates Pin
Mike Gavaghan20-Jul-07 8:14
Mike Gavaghan20-Jul-07 8:14 
QuestionExternal Data Type Classes? Pin
Willem Fourie19-Jul-07 22:09
Willem Fourie19-Jul-07 22:09 
AnswerRe: External Data Type Classes? Pin
Henrik Jonsson31-Dec-07 0:48
Henrik Jonsson31-Dec-07 0:48 
GeneralAdding Events Pin
asfokasjdfashjhwernkbweqpoiruweqeirhasfasm18-Jul-07 5:55
asfokasjdfashjhwernkbweqpoiruweqeirhasfasm18-Jul-07 5:55 
GeneralRe: Adding Events Pin
Mystic Taz8-Oct-07 21:08
Mystic Taz8-Oct-07 21:08 
GeneralRe: Adding Events Pin
Henrik Jonsson31-Dec-07 1:08
Henrik Jonsson31-Dec-07 1:08 
GeneralVery Nice Pin
Paul Conrad17-Jul-07 19:21
professionalPaul Conrad17-Jul-07 19:21 
GeneralOutstanding Pin
jackmos17-Jul-07 7:59
professionaljackmos17-Jul-07 7:59 
GeneralFamiliar Code Pin
Dmitri Nеstеruk17-Jul-07 5:35
Dmitri Nеstеruk17-Jul-07 5:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.