Search code examples
wpfcommandundocommand-patternredo

Keeping track of parameters using a Command Pattern and ICommand for Undo/Redo. Storing multiple commands?


I've been working on a WPF application that involves moving many shapes. It is mostly MVVM and relies heavily on commands. I had not worried about Undo/Redo until just recently. I don't think it will be too difficult since most of my changes involve commands that inherit a base class CommandBase that implements ICommand.

So far I have added another Interface named IUndoCommand that uses ICommand. I have added an Undo method that will perform the operations needed when an undo is called.

I will be using a stack for both Undo and Redo, but I'm running in to an issue with the parameters for the Execute/Undo methods. Is there a proper way to store these parameters of type object? Is it advisable to add a field/method to IUndoCommand? If so should I set it in the Execute method or in a constructor (if I even can.)

If not should I pass it as it's own object in the Stack?

Secondly, (although this can probably be it's own question) is there a better data structure to track multiple commands? I currently have a loop that runs multiple commands to move multiple selected shapes and would like to allow one undo to undo them all. I guess I could convert this to a command on it's own and pass commands to it, but again I'm new to this and would rather do it right.

Thanks for reading and any help would be greatly appreciated.

Sources:

Code Project VisualStudioMagazine StackOverFlow


Solution

  • Since the interface doesn't need access to the data (it should just need an Undo()/Redo() method pair, and potentially a flag for whether it can undo), it doesn't need to know about the parameters at all.

    One option might be to make your implementation of IUndoCommand generic. You could then use this to store the parameter in a type-safe manner.

    Your CommandBase class could then be generic, ie:

    class CommandBase<T> : ICommand, IUndoCommand
    {
        // You could then store the parameter directly...
        public T Parameter { get; private set; }
    }