Undoing changes to related objects using C #

In my application, I have a set of data objects that determine what types of data the application collects when it runs.

A user can open a dialog box for editing these objects, and this dialog box contains DataGridView instances bound to collections. This means that any changes made by the user are instantly applied, which is not very good.

Another problem is that there is a Cancel button in this dialog box, which allows the user to discard all changes that they have made since the window was opened.

Currently, when the window is open, I serialize all objects (a shallow copy does not work), and if the user clicks the Cancel button, I will deserialize them to restore them. The problem I am facing is that it is messy. It changes all links, and some of these objects also store a data history that is not serialized. Then I have to have events breaking through the application to notify objects when their links are updated, etc. It is a pain.

Is there a better approach to this problem?

+4
source share
2 answers

There is a better way using the interface that is prepared in the framework - IEditable

Beginedit
CancelEdit
Endedit

The basic idea is that you create a snapshot of some state of the object when calling BeginEdit. On CancelEdit, you return to this SavedState, and in EndEdit you commit it.

The devil is in the details, of course. Here's a popular link that served as an answer to implementing similar SO questions for some ideas

http://www.paulstovell.com/blog/runtime-ui-binding-behavior-ieditableobject-adapter

Cheers
Berryl

NOTE: this is not conceptually different from what Tokko says, and you should give him an answer. BUT should be described in a separate answer, because it formalizes the concept in .Net in an idiomatic form and offers a deeper understanding of the useful implementation. It’s also fun to say the word idiomatically :-)

+3
source

You can work with the original version and a copy of your object or with a copy of your entire collection.
This way you can edit the copy and save to make changes or undo to save the original object / collection.

Like this

0
source

All Articles