An easy way to revert changes in Object data

No Comments »

A quite common problem in an MVC GUI is the need to revert changes in the model made by the user. Let’s say you create a UI where the user can modify some fields that are bound to a model object. After the user already did some changes, he recognizes that he entered some wrong data and wants to cancel the editing. So what now? The data is already written into the model.

The easiest way to revert this changes is obviously to let the user work with a copy of the real data object. So let’s add a clone method to the model and mark it as Serializable.

Model with Clone() method

[Serializable]
public abstract class PersonModel {

    public string Name { get; set; }
    public string FirstName { get; set; }
    public int Age { get; set; }

    public PersonModel Clone() {
        IFormatter formatter = new BinaryFormatter();
        Stream stream = new MemoryStream();
        using (stream) {
            formatter.Serialize(stream, this);
            stream.Seek(0, SeekOrigin.Begin);
            return (PersonModel)formatter.Deserialize(stream);
        }
    }
}

We now just bind the copy of the object to the view:

myView.DataContext = person.Clone();

This copy can easily be dropped in case the user cancels the editing. In case he saves the
changes, you can just replace the original object with the copy.

One problem I ran in it was, that I normally implement the INotifyPropertyChanged interfaces to my models to inform the view about changes in the model. Unfortunately the PropertyChangedEventHandler contained in this interface causes a nasty SerializationException because the Class PropertyChangedEventManager is not marked as Serializable.

System.Runtime.Serialization.SerializationException was caught
Message=”Type ‘System.ComponentModel.PropertyChangedEventManager’ in Assembly ‘WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35′ is not marked as serializable.”

The easiest way to work around this problem is to just exclude the PropertyChangedEventHandler from the serialization.

Clonable Model that implements INotifyPropertyChanged

[Serializable]
public abstract class PersonModel : INotifyPropertyChanged {
    [field: NonSerialized]
    public event PropertyChangedEventHandler PropertyChanged;

    private string name;
    private string firstName;
    private int age;

    public string Name {
        get { return name; }
        set { name = value; OnPropertyChange(); }
    }
    public string FirstName {
        get { return firstName; }
        set { firstName = value; OnPropertyChange(); }
    }
    public int Age {
        get { return age; }
        set { age = value; OnPropertyChange(); }
    }

    public PersonModel Clone() {
        IFormatter formatter = new BinaryFormatter();
        Stream stream = new MemoryStream();
        using (stream) {
            formatter.Serialize(stream, this);
            stream.Seek(0, SeekOrigin.Begin);
            return (PersonModel)formatter.Deserialize(stream);
        }
    }

    protected void OnPropertyChange() {
        string propName = new StackFrame(1).GetMethod().Name.Replace("set_", "");
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }
}

In this way everything works fine and you have hopefully one problem less to care about…

Happy DataBinding :-)

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • MySpace
  • Twitter

Comments are closed.