.net - Undo Redo in WPF/C# in an already functional application -


i have done research how can achieve title of question. app working on has been under development couple of years or (slow progress though, know how in real world). requirement me put in undo/redo multiple level functionality. it's bit late "you should have thought before started" ... well, did think - , did nothing , here is. searching around (and external links) can see 2 common methods appear ...

command pattern

memento pattern

the command pattern looks hell of lot of work, can imagine throwing thousands of bugs in process don't fancy one.

the memento pattern lot had in head this. thinking if there way take snapshot of object model in memory, able store somewhere (maybe in memory, maybe in file). seems great idea, problem can see this, how integrate have written. see app have draws images in big panel (potentially hundreds) , allows user manipulate them either via ui or via custom built properties grid. entire app linked big observer pattern. second changes, events fired , needs update does. nice cant thinking if user entering text texfield on properties grid there bit of delay before ui catches (seems everytime user presses key, new snapshot added undo list). question ....

  • do know of alternatives memento pattern might work.
  • do think memento pattern fit in here or slow app down much.
  • if memento pattern way go, efficient way make snapshot of object model (i thinking serialising or something)
  • should snapshots stored in memory or possible put them files?

if have got far, thankyou kindly reading. input have valuable , appreciated.

the critical things undo/redo are

  • knowing state need save , restore
  • knowing when need save state

adding undo/redo after fact painful thing - (i know comment of no use now, it's best design support application framework before start, helps people use undo-friendly patterns throughout development).

possibly simplest approach memento-based one:

  • locate data makes "document". can unify data in way forms coherent whole? if can serialise document structure file, logic need in serialisation system, gives way in. down side using directly usually have serialise undo huge , slow. if possible, refactor code (a) there common serialisation interface used throughout application (so , every part of data can saved/restored using generic call), , (b) every sub-system encapsulated modifications data have go through common interface (rather lots of people modifying member variables directly, should call api provided object request makes changes itself) , (c) every sub-portion of data keeps "version number". every time alteration made (through interface in (b)) should increment version number. approach means can scan entire document , use version numbers find parts of have changed since last looked, , serialise minimal amount save , restore changed state.

    • provide mechanism whereby single undo step can recorded. means allowing multple systems make changes data structure, , when has been updated, triggering undo recording. working out when may tricky, can accomplished scanning document changes (see above) in message loop, when ui has finished processing each input event.

beyond that, i'd advise going command based approach, because there many benefits besides undo/redo.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -