Undo/redo + save
I'm developing a C# application that has the following two requirements:
- the user should be able to undo/redo commands such as inserting, editing and deleting accounts, transactions, etc.
- these commands should only be persisted to the SQL CE database when the user clicks save.
Are there any best practices for doing this? I have read that implementing the command pattern is useful for the undo/redo requirement. The pro开发者_如何学运维blem for me comes in persisting the changes to the database. I am using business entities and not DataSets, and I cannot use Linq to SQL or Entity Framework, so I am not sure how to track changes made to these entities to persist to the database when the user clicks save.
My question is:
Is it advisable to implement the Unit of Work pattern to track changes or is there a better way? I am not sure how that will combine with the command pattern, e.g. is the unit of work passed to the command which marks an entity as dirty/new/deleted, and what happens when the undo method of a command is executed? Is the entity marked as dirty/new/deleted again or can it be removed from the unit of work somehow?
http://en.wikipedia.org/wiki/Memento_pattern
Command will change your business entities, they will store current state in state objects, and list of previous state entries, save will save current state, rollback will restore previous state.
Use the Command Pattern with the following change (in the regular command pattern you have an invoke method. In this specialization you use a dispatcher to invoke the command).
Create a Dispatcher class which all commands are invoked through. Create an event in that class which is invoked for all commands. Create a class which listens on that event. Let that class save serialize all changes to your database.
精彩评论