Editing an object and the isDirty () flag

I am working on a system, since the user can edit existing objects (more precisely, "Filter" objects) through a graphical interface. As a hint to the user interface, we only want to enable the save button if the user has really changed something for the object. I was wondering if anyone has any experience with this problem and how best to approach this.

I was thinking about adding the isDirty () flag to a domain object. When the user starts editing the filter, I would then make a copy, transfer it to the graphical interface and give the user the opportunity to make changes to the copy. After that, binding to the isDirty () flag activates / deactivates the save button. When saving, the differences will then be merged into the original object and saved.

Additionally, I thought what would happen if the user discards the changes made by him to the object. The isDirty () flag should return false. Therefore, I assume that the only way to achieve this is to keep the original value of each property inside the domain object.

Any ideas?

+7
user-interface dns flags
source share
7 answers

Correctly!

In addition, you can open two methods: BeginEdit - in this method, your IsDirty Flag is True. This means that you are doing a modification. Call this method when you are about to make changes.

CancelEdit - in this method reset the IsDirty flag for False. This means that you have edited the editing process and returned to its original state. Call this method to discard any changes made.

And as soon as any modifications are saved, you will also reset the IsDirty flag to False.

Hope this helps.

+3
source share

If you are using the .NET framework, you can take a look at the Rockford Lhotka CSLA.NET platform: http://www.lhotka.net/cslanet/Default.aspx

CSLA is a mature structure that includes object state management (IsDirty), functionality cancellation, data binding and much more, plus it is free and open source.

+2
source share

There are several interfaces that can be implemented using change tracking and cancellation: INotifyPropertyChanged and IEditableObject. Both of these interfaces allow the object to play well with data binding.

public class Person : INotifyPropertyChanged, IEditableObject { private bool isDirty; public bool IsDirty { get { return isDirty; } } private string firstname = string.Empty; public string Firstname { get { return firstname; } set { if (firstname == value) return; firstname = value; NotifyPropertyChanged("Firstname"); } } private string lastname = string.Empty; public string Lastname { get { return lastname; } set { if (lastname == value) return; lastname = value; NotifyPropertyChanged("Lastname"); } } public event PropertyChangedEventHandler PropertyChanged; public void NotifyPropertyChanged(string propertyName) { isDirty = true; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } private bool inTrans; private Person copy; public void BeginEdit() { if (!inTrans) { if (copy == null) copy = new Person(); copy.isDirty = isDirty; copy.Firstname = Firstname; copy.Lastname = Lastname; inTrans = true; isDirty = false; } } public void CancelEdit() { if (inTrans) { isDirty = copy.isDirty; Firstname = copy.Firstname; Lastname = copy.Lastname; inTrans = false; } } public void EndEdit() { if (inTrans) { copy = null; inTrans = false; } } } 
+2
source share

If you have a set of objects that are being edited, you will probably need something more than the isDirty () boolean flag. This problem is no different from reference counting, i.e. Increases the number of errors when editing and decreasing to cancel. If you support cancellation, I suspect that you are going to get pretty hairy logic. I would save it from your domain objects.

+1
source share

Yes, it works well. Instead of undoing, I use the IsDirty method to indicate that something MAY change the record, and then it launches my "record-changing logic". I developed my own structure, where each field of the table is actually a property of the object. Each time a field is written to objects, the "isDirty" flag is set. In the "SaveObject" method of the object (actually it is a helper class, but it can easily be in the object, but I need the ability to save objects in different ways, for example, in xml, database, etc.), I check IsDirty and if it false, I will skip saving. This simplifies the logic, because every time I had the opportunity to change the object, I call SaveObject and let it handle the framework.

+1
source share

Depending on your domain, you can use equality to check for differences. Save the original object and make a copy of the object for editing. Anytime editing can be done, change the user interface accordingly.

The advantage of this proposal is that it does not bind specific GUI functions (isDirty () flag) to domain objects, but YMMV

+1
source share

If you support cancellation of an operation at a level of detail greater than "cancel all since last save", I suggest canceling the stack. When something is edited, it (or it cancels the action of the functor or delegate) is pushed onto the stack. When you cancel, you simply pop out the stack and cancel the operation. Your isDirty () flag is just a check to see if there are elements in the undo stack, and not additional storage and logic to update.

+1
source share

All Articles