Personinstance that is loaded from a persistente storage, such as a database, the
Personinstance can be directly bound to the UI but it requires us to implement the
INotifyPropertyChangedinterface and if we want to enable it for the
ChangeTrackingServicewe need to inherit from a base class. Both are not an option when dealing with the Single Responsibility Principle and with POCO objects.
PersonViewModelthat will be responsible to enrich the Person with property change notification support and with change tracking capabilities;
EditorViewModelthat will allow a clean separation of responsibilities owning all the relationship with the memento.
Personclass like the following:
Personclass are basically the same, we can say that this is corner case, most of the time in real scenarios there will be a huge difference between the model and the editing view model.
Initializemethod, for the sake of the sample we can do the same thing using a constructor, using a
Initializemethod allows us to easily resolve
PersonViewModelinstances using an inversion of control container without the need to deal with the currently edited
Personruntime instance. At initialization time we are doing 2 important things:
RegisterTransient()method of the base class to register the current instance as transient, if required; To dive into the meaning of a transient entity look at the Change Tracking Service API;
SetInitialPropertyValue()method to initialize the default value of the
PersonViewModelproperties without affecting its tracking state;
ViewModelwe can build the editor:
ChangeTrackingServiceinstance, that in this specific sample is bound to the editor itself. In the constructor we are setting up a MementoObserver to watch the memento instance and we are binding that observer to 2 commands whose role is to expose Undo/Redo functionalities to the UI. Last we create a
Personinstance, in real scenarios the
Personinstance is expected to arrive from a persistent storage or a remote resource, we create the
PersonViewModel, attach it to the memento service and finally initialize it with the person data source.
PersonViewModelinstance to the