Search code examples
design-patternsrepositoryundocommand-pattern

Persistent Command pattern


what I am trying to achieve is to have a persistent list of "undoable" changes on a persistent storage (database).

The architecture employs repositories for the domain objects and Unit of Work for transactions and for the final part (undo) I thought of using the command pattern. However, for me there seems no good solution how to make the executed command persistent.

Basically, there are 3 write-operations on repositories add/update/delete and with the command pattern I would need to store the state before the command was executed. For example: I have to store the domain object (entity) before I delete it so that I can restore it once the undo is called on the command. The big question here is how to store the before-state in a neat way!

Maybe someone of you guys came across the same question which in my mind is not that uncommon.

Thanks, Chris


Solution

  • The different methods that I have come across are:

    1. Store the complete domain entity before the change. This might require a complicated schema design and object-relational mapping.
    2. Store the complete domain entity before the change using an additional set of tables to hold old values.
    3. Serialize the complete domain entity before the change and store it as a BLOB or XML string.
    4. Store the change to the domain entity in a way that the undo operation can be constructed from the change. If add and update operations can create complex object graphs then you still need one of the above approaches to store the changes.