What is MVVM to handle insertion

Here is my requirement (I am simplifying things for the sake of this question). I have a list of people identified by first and last name. The list is stored in an XML file. I need to create a WPF application consisting of two windows: the main windows display a list. Nothing unusual, just a list with first and last name. Three buttons below: Add, Change, Delete. When you click the Delete button, the selected line is permanently deleted from the file. When you click the "Add" or "Edit" button, a second window (dialog) appears in which you can respectively enter a new person by entering his name and surname, or change and save the existing entry.

How do I create this application in MVVM? Here are the specific questions I have:

1) From an MVVM point of view, I cannot directly bind to an XML data source, or can I? So I have to create a Person object that implements INotifyPropertyChange and then save a bunch of these objects in an ObservableCollection. Accordingly, I need to write code that reads an XML file and converts it into a collection of objects, and vice versa, when I need to save the changes back to the file. I'm right?

2) Where can I put the implementation of the Persist method, which actually saves to an XML file? There are two places where this method will be called: click the "Delete" button on the main window or click the "OK" button on the "Add / Edit" window.

3) Please note that the same window handles "Add and Edit", how do I implement this window properly with MVVM? How to display the title of this window "Add" or "Edit" without resorting to ugly, if the code is expressed? To what I associate with the two text fields that I have (name and surname). Also note that in the "Add / Edit" window there are "OK" and "Cancel" buttons, changes are not accepted until the "OK" button is pressed, and if you click "Cancel", the changes should be discarded. One of the possible solutions that I see is to clone the Person object, and not directly contact the same object as the linked list, so that the list does not show changes until the OK button is pressed, but itโ€™s hairy , since I need to write code to copy properties back and forth, is there a better way to do this in MVVM?
thanks in advance

Henry

+4
source share
1 answer

To solve three specific issues,

1 / If your view is linked directly to XML, this is not MVVM. The MVVM template will be bound to the ViewModel that represents the XML. If you do not need to modify XML, then there is no problem with direct binding, but since you are going to add / edit / delete values โ€‹โ€‹from XML, it makes sense for the ViewModel to control this interaction. I would really create a Person object that implements INotifyPropertyChanged.

2 / In MVVM, all functional code is sent to the ViewModel. This Persist button launches a command that calls a method in the ViewModel. At first, this is somewhat confusing, and not as simple as the 'click' code, but it makes more sense from a dumb point of view / MVVM. It is also cleaner.

3 / The "Edit" screen will be attached to the Person object and will shoot at the Save command. Then this command executes the corresponding logic, determining whether it is a new (insert) or existing (updating) record. If the object is new, then the parent ViewModel (you will need a ViewModel for the collection, not just an ObservableCollection) to catch this event and add a new object to the collection.

In addition, the โ€œEdit / Addโ€ window can return a result, and the parent ViewModel can examine this result after closing the window, then determine what to do (save / delete, edit / add, any other check, etc.).

Here are some links:

+2
source

Source: https://habr.com/ru/post/1396123/


All Articles