Making Your Model Recordable

To make an object usable for undo/redo operations, you will need to add the RecordableAttribute aspect to the class. This aspect instruments changes to fields and records them into a Recorder. The aspect also instruments public methods to group field changes into logical operations.

This topic contains the following sections:

Making a class recordable using PostSharp Tools for Visual Studio

To make a class recordable using PostSharp Tools for Visual Studio:

  1. First place the caret on the name of the object that you want to make recordable. The smart tag will appear below the object name. Expand it and select "Add another aspect...".

    Add Undo Redo
  2. In the Add Another Aspect wizard expand the Model section, select "Enable undo/redo", and click Next.

    Undo Redo Wizard
  3. Once the wizard has completed the object will now be flagged as recordable.

    C#
    [NotifyPropertyChanged]
    [Recordable]
    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
    }

    By adding the RecordableAttribute aspect to the Person class all of the properties that are primitive types will be recorded when they change.

Making a class recordable manually

To make the class recordable manually:

  1. Install the PostSharp.Patterns.Model package using NuGet.

  2. Add the RecordableAttribute to the class.

Working with object graphs and collections

In the example above, the Invoice class just had a few fields of a primitive type (int, string, ...). In real-world application, objects are not isolated entities, but are parts of larger structures named object graphs.

The RecordableAttribute aspect needs to understand the parent-child structure of your object graphs. It relies on the AggregatableAttribute aspect for this purpose.

Therefore, when adding the RecordableAttribute aspect to a class, you need to complete a few more steps:

See Parent/Child Relationships for more information.