Data transfer to the data layer

My model consists of one main object, which the user can add other objects. Added objects are stored in List<object> , contained in the main subject, and the related child objects.

So, if the project is the house. The user can add several Room objects to the house, which are stored in the List<Room> RoomList . Each Room can then add a few added Furnishings to each room. List<Furnishing> FurnishingsList

The question is how to implement this in the MVVM approach? When the user adds a new object, do I add it to the ObservableCollection in the ViewModel so that the view is updated and at the same time adds it to the model connected to the virtual machine? Or do I store it in a virtual machine until it is saved or committed, and then pass it to the Model?

In my example, I have different editors (each of them is a user control). Thus, the user can edit the house at a high level, using one editor to add, edit and delete rooms from the house. And at a lower level, using a different editor to edit each room, adding and removing Furnishings .

So, when the user "Edits" the room. I create an EditRoomModelView containing the specified Room . The user adds, edits and otherwise manipulates the furniture in this room.

In each team, it is best to synchronize the data in Model and ModelViee, as described above. Or I will write the changes directly to the Model, and the ViewModel only provides getters on the nested properties of the model. Thus, however, adding objects to model lists does not update the view. I really would have to add data to both the ModelView and the model at the same time, so that everything is in the same state.

Applications for rambling, struggling to find a good approach to this, can anyone understand what I'm cloning about?

+4
source share
3 answers

I am not sure there is one good answer. It sounds like you are discussing how to maintain consistency - do you have a simple end-to-end diagram at the presentation level, or do you insert user requests and have logic there.

If this is, in fact, a question, there are different factors. Do you prefer to do presentation-level verification? How complex is the application and how scalable is it? Are your view models available in different places?

So, with all this in mind, I can tell you how personally I prefer to approach this problem (in particular, the question of adding a mention to you). I usually like to create view models that describe user problems using generics like EditItemViewModel<T> or ListViewModel<T> . So, in the graphical interface there will be some list of numbers associated with the ListViewModel<Room> . This virtual machine obviously exposes an observable collection, as well as commands for adding, deleting, editing.

So, from my point of view, the level of presentation, the model material that this virtual machine is going to do, is route requests for other GUI tasks. If you click "Add" to add a room, this virtual machine is responsible for initiating a request using the command for any screen / window / everything that is needed to add a room that will have its own virtual machine. After receiving the request for addition, this virtual machine transfers the generated data transfer object to the domain where validation will be performed, and any domain operations are necessary. I usually process this through the service level. Now, if the domain operation was successful, the service level will raise an even or callback to inform the VM that there is a change. When this happens, the VM list re-requests its service and accordingly updates its observed collection. Now your GUI is compatible with the domain in all directions.

The reason I approve of this type of layered approach is that all business logic arises in the "below" the GUI, and the GUI should not touch on this event. Conceptually, the GUI simply says: "Here, at the domain level, the user wants to add this - do it all, and let any interested GUI components know when you are done so that they can be updated."

What I am describing here will naturally require some overhead compared to a simple, end-to-end scheme or a scheme in which virtual machines simply re-map properties on some model object. But, personally, I think that the advantage obtained by decoupling is worth it, especially when expanding the application. This gives you the opportunity to fundamentally change the internal interactions of the domain model, without changing the code of the presentation level, lick.

+3
source

First: View is a mirror of your DataModel , so always add a Model first and only after you think about whether it clicks on Vm or View .

Second: ViewModel is a bridge that connects your Model to View , but does not mean that it should be structured in the same way as Model . If you want to show all child collections to the user, you must expose them as properties and build their parent-child internaly relationship in Vm or in Vm only have raw collections and have effective relationships between them in the DataModel .

+2
source

In the described application, this sounds like your view model layer does not greatly change or form your model. Thus, I would make various property-model get-models adapters for objects of simulated objects. Thus, your model will be updated immediately when the view model changes.

However ... if you need to provide "cancel" functionality, that is, the user opens a modal form to edit some presentation model, but then cancels, rather than OK, editing, this simple approach will not work.

The timing associated with fixing the level of the model is rarely of great importance, since it usually does not take much time. More importantly, when the state of the model is saved. This is often caused by an explicit invocation of the save command, which sends the model to the save service (file / cloud, etc.)

+2
source

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


All Articles