I have a
I'm aware that I can do this via the
return this.currentState != this.initialState;
// this._hasChanged is set to true via event handlers
You'll have to implement the
INotifyPropertyChanged interface from within your object classes, then catch whenever a change occurs through proper event handlers for your type class within your
The one object offering what you require is the
DataSet, containing both the Original and Current (changed) state of an persistent entity. Then, when one cancels, all you need to call is the
Rollback() method. When one accepts the changes, then a call to the
AcceptChanges() method will do.
DataSet, perhaps considering an ORM like NHibernate will do the job for you, plus allowing you to use custom defined objects, instead of a
DataSet. Keeping the
ISession API alive while in your form will allow the ISession to keep track of your changes whatever it may be to whatever object it is, as long as it is know by NHibernate.
Another solution implementing the
INotifyPropertyChanged interface, is at the property setter, you could stock the Original value within a private field or for every property of an object. You could simple have an abstract class with the
HasChanges property return whether each property is as its Original state, then return true or false accordingly.
I have a question regarding our interesting initial discussion. I just want to make sure of one thing. Let's call it language barrier if we like. But publishing the
PropertyChanged event through the
INotifyPropertyChanged interface will also somehow "rollback" an object to its original state. The only detail you had to take care is that if the user says he doesn't want to keep the changes, then reload this CurrentItem from the underlying database via the
BackgroundWorker class and its done! No lagging from your GUI, your user has canceled the changes, and you resetted the object to its default/original state!
Well, I guess here's enough details to make yourself an idea, plus all of the other good answers provided by the others. I am confident you will find your way to accomplish what you want.
Best of success! =)