8

Can you think of a scenario where IEditableObject would be still usefull in an MVVM-based WPF application? If so, do you have an example that demonstrates this.

bitbonk
  • 48,890
  • 37
  • 186
  • 278

3 Answers3

16

I have used IEditableObject in one of my applications. For example if you have a dialog for editing something, you could implement IEditableObject on your ViewModel. You call BeginEdit() when the dialog opens, EndEdit() when the user clicks OK, and CancelEdit() when the user clicks cancel.

IEditableObject is a good interface anytime you want to be able to roll back changes.

Botz3000
  • 39,020
  • 8
  • 103
  • 127
  • Do you implement the methods in each ViewModel class or just in the original abstract class? If the original class, how do you do it there. I was thinking about fetching the model again from database but the models don't have joint parent class. I'm not sure about the reflection method because I only edit the model part. – Ingó Vals Feb 20 '11 at 13:41
  • 1
    It depends. I would only implement it in the abstract base class if the models have common functionality to run on these events. If so, you could implement it there, but inside it, call an abstract method, like OnBeginEdit(), that deriving classes need to provide. That way you have both common and specific functionality covered. – Botz3000 Feb 21 '11 at 07:25
  • I have a ViewModel which is a collection of objects. I want to use a dialog to edit the selected gridview item. Which implements `IEditableObject`, the View containing the grid, the ViewModel to which the grid is bound, or the class that the ViewModel contains a collection of? – Mark Richman Jun 05 '14 at 12:13
  • @MarkRichman I would say the class that the ViewModel contains a collection of, because it's the only place where it makes sense. BeginEdit, EndEdit and CancelEdit map very well to a modal dialog with OK and Cancel buttons. – Botz3000 Jun 06 '14 at 06:20
3

I've got an implementation of IEditableObject in my application so that I can keep from updating my data model if what the user has entered is invalid, and roll back to the original values if the user abandons changes.

Robert Rossney
  • 94,622
  • 24
  • 146
  • 218
2

Within a type being displayed in a DataGrid. This was needed since when I am making use of tabs and a DataGrid is being stored within that tab switching the tabs needed to force a commit so to speak within the DataGrid if a cell was active; rolling back the changes since they were not committed. T

There is a behavior being applied to the DataGrid to achieve this functionality but the IEditableObject portion is below.

private IDatabaseConnection _copy;

void IEditableObject.BeginEdit()
{
    if (this._copy == null)
        this._copy = _container.Resolve<IDatabaseConnection>();

    _copy.Database = this.Database;
    _copy.DisplayName = this.DisplayName;
    _copy.HostName = this.HostName;
    _copy.Username = this.Username;
    _copy.Password = this.Password;
}

void IEditableObject.CancelEdit()
{
    this.Database = _copy.Database;
    this.DisplayName = _copy.DisplayName;
    this.HostName = _copy.HostName;
    this.Username = _copy.Username;
    this.Password = _copy.Password;
}

void IEditableObject.EndEdit()
{
    _copy.Database = String.Empty;
    _copy.DisplayName = String.Empty;
    _copy.HostName = String.Empty;
    _copy.Username = String.Empty;
    _copy.Password = String.Empty;
}
Aaron McIver
  • 24,527
  • 5
  • 59
  • 88