IEditableObject and Catel

Dec 2, 2011 at 3:40 PM


In the Person sample of Catel, both the Person class (=the Model) and the PersonViewModel (=the ViewModel) implement IEditableObject.The sample also uses ViewModelToModel attributes.

As such, if you select one of the persons from the list and trigger the "Edit" command, you can edit e.g. the name in the DataWindow that opens, and as you type, you also see the persons name in the list on the mainpage updated. If you then press <Cancel> the changes in the list on the mainpage get undone (expected behavior).

If, on the other hand you use a plain Person class that does not implement IEditableObject, the changes don't get undone.

Is this a bug?

I would expect that when I start editing, I am editing the PersonViewModel, so as it implements IEditableObject it's BeginEdit would be called (making a copy of its current state) and when <Cancel> is pressed, its CancelEdit would be called (rolling back the changes from the copy). I would also expect that, as this rolling back is changing the properties of the PersonViewModel, these changes would also be pushed back to the Person (=Model), so it's changes would also be automatically undone without the need of implementing IEditableObject.

That way, it would be possible to use stub classes generated by "add service reference" to a WCF service as Model classes in Catel (as these only automatically implement INPC and not IEditableObject).

Dec 2, 2011 at 4:51 PM

No, there is no way Catel knows on how to revert the changes. Only when an object implements the IEditableObject, it can let the object know to revert. This is because at first, reverting to previous versions seem easy, but think about larger graphs that have multiple internal collections of objects, etc.

The ViewModelToModel attribute directly updates the model (especially to allow the implementation of validation only once). In 2.5, a new option will become available that allows the on-demand mappings from viewmodel to model and vice versa. But, for now if your model does not support versioning, you need to map the properties to the VM manually and back on commit (this is actual MVVM logic, not the direct binding to models that you see happen a lot).

Dec 2, 2011 at 4:56 PM

OK, thanks for explaining. 

Dec 16, 2011 at 1:08 PM

Feature is implemented, you can now set the Mode on a ViewModelToModel mapping. The latest beta is available here: