Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IEditableObject in MVVM

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.

like image 757
bitbonk Avatar asked Dec 10 '10 15:12

bitbonk


3 Answers

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.

like image 174
Botz3000 Avatar answered Oct 18 '22 02:10

Botz3000


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.

like image 32
Robert Rossney Avatar answered Oct 18 '22 01:10

Robert Rossney


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;
}
like image 42
Aaron McIver Avatar answered Oct 18 '22 03:10

Aaron McIver