MVVM, where does the code for loading data belong?

When I wrap my head around mvvm, the view is a view, and the viewmodel is a β€œmodal view”, and the model is the entities we are dealing with (or at least this is my understanding). But I do not understand what and when the model objects are populated. For example:

Suppose I have an application that should create a new record in the database. And this entry should start with the default values. Who is responsible for the new record and gets the default values. Does this have anything to do with MVVM or is it part of the data access layer? Who brings up the view mode?

Or for existing records, when \ where are records stored? And saved if changed?

thanks

+4
source share
3 answers

In a too simplistic answer, your ViewModel should contain LOGIC to control the display of your view, as well as how it is allowed to interact with the model or data.

Events, such as receiving data, saving and deleting, are intercepted through the command mechanism and placed in the ViewModel, where they can be tested. Handling dirty events is also a ViewModel responsibility. As for who calls the ViewModel, you trust the Calling to Binding features available in WPF and Silverlight.

In the ViewModel, he is still looking for best practices and ensuring that you have a DataAccess layer abstracting your data source and possibly using a repository template to abstract it.

The ViewModel life cycle can be as simple as the following:

  • Designer called by view
  • GetData Method Called by ViewModel Ctor
  • Data Received and Transferred to an Existing Binding Data View ObservableCollection Property

However, since you are likely to have many moving parts inside the Ctor VM, including the Data Repositories interface, you probably want to work with IoC. This will make the ViewModel life cycle closer to ...

  • View / ViewModel (depends if you are viewing or viewing the Model first) pulled from IoC
  • IoC Handles View-ViewModel (Based on Convention)
  • Data repository embedded in ViewModel
  • GetData Method Called by ViewModel Ctor
  • Data Received and Transferred to an Existing Binding Data View ObservableCollection Property

This may seem like more steps, however with the IoC container you really just call one method, such as IoC.Get (), and the rest of the steps are automatically connected based on the conventions applied.

+4
source

I use view models to control loading (by default) and save my models, as well as to create collections and objects that I use to bind to my views. This includes setting defaults for my models.

0
source

The state and behavior of your view is defined in your view model, which means that all events are described here.

Who brings up the view mode? it depends on who needs it. You can call it in your submission.

for existing records, when \ where are the records stored? And saved if they are changed? saving and retrieving details is in your view model.

A detailed explanation can be found on the website .

0
source

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


All Articles