This is the online documentation for PostSharp 5.0.
Download PDF or CHM. Go to v4.3 or v5.0

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.

To make the class recordable:

  1. Add the PostSharp.Patterns.Model package to the project.

  2. Add the RecordableAttribute to the class.

  3. Annotate your object model for parent/child relationships as explained in Parent/Child, Visitor and Disposable.

Example

The following example shows an object model that has been prepared for undo/redo.

C#
[Recordable]
public class Invoice
{
  public Invoice()
  {
     this.Lines = new AdvisableCollection<InvoiceLine>();
  }

    [Reference]
    public Customer Customer { get; set; }

    [Child]
    public IList<InvoiceLine> Lines { get; private set; }

  [Child]
  public Address DeliveryAddress { get; set; }
}                

[Recordable]
public class InvoiceLine
{
  [Reference]
  private Product product;

  public decimal Amount { get; set; }

  [Parent]
  public Invoice ParentInvoice { get; private set; }
}     

[Recordable]
public class Address
{
}

[Recordable]
public class Customer
{
}

By adding the RecordableAttribute aspect to the classes, all modifications to properties of these classes (including modifications to child collections) will be recorded.