How does MVVM actually work?

Asked

Viewed 699 times

5

How does MVVM actually work? What are the responsibilities of Viewmodel and Model?

I’m implementing a standard project MVVM some time ago, but now some questions have arisen about the.

Where to implement the interface INotifyPropertyChanged? I learned to implement it in ViewModel, but I see many examples implementing directly in Model.

Another question would be where to implement a code of the type in the example, in the viewModel or Model. I would for example define the value of a int and would save in Backend shortly after.

public class ModelExample
{
  int items {get; set;}
}

public class ViewModelExample
{
  ModelExample modelExemplo {get; set;}

  ViewModelExample(ModelExample modelObject)
  {
    modelExemplo = modelObject;
  }

  //Implemento um metódo desse tipo na ViewModel ou na Model?
  public async Task Adicionar(){
    modelExemplo.Items++;
    //Salva no Backend do Azure (para ilustar o exemplo)
    AzureMobileServices.Instance.SaveDataAsync(modelExemplo);
  }
}

  • 2

    This should help a little: http://answall.com/q/21539/101

  • The standard says that the interface should only be implemented in Viewmodel. But because I am asking questions I usually put in the model too... Whether you will do the same or not depends on your experience and need.

  • Viewmodel represents the behavior of the screen. Inotifypropertychanged should be implemented on all objects that want to notify property changes. This is very common in Viewmodels and Models. In your example, the add method should be an Icommand, which is the abstraction of actions that will be executed in Viewmodel. This action will be linked to a button, for example, via Binding. The most interesting thing is that, it is possible to test your Viewmodels without running the App. It is possible to apply Unit Tests to Viewmodels, and for me this is the great differential!

1 answer

2

MVVM (Model-View-Viewmodel)

As in this reply already has the definition and a basic explanation about MVVM, I will not go into too much detail. I will focus more on your question.

For contextualization, MVVM is a UI-based design standard, it is an application of MVP, which is a derivation of MVC. The main MVVM obejectives are: Flexibility, Ease of maintenance, Modularity, Testability and Rich UI (rich interface).

How does MVVM actually work? What are the responsibilities of Viewmodel and Model?

This image, taken from Devmedia, shows the MVVM architecture:

inserir a descrição da imagem aqui

To View is responsible for defining the appearance or structure the user sees on the screen. With the property DataContext she attaches herself to ViewModel.

To ViewModel is responsible for making available to the View a presentation logic. It has no specific knowledge about the view, or how it is implemented, nor its type. It implements property and commands so that the View can fill in your controls and notify you if there is any change in status. It implements the interface INotifyPropertyChanged

The Model is responsible for encapsulating business and data logic, being the domain model of an application. It is also responsible for the validation of the data. It does not directly refer to View or ViewModel. An interesting feature of model is that it provides state change notification events through the interfaces INotifyPropertyChanged and INotifyCollectionChanged, facilitating the filling of data in the View.

Where to implement the interface INotifyPropertyChanged?

In my opinion, the INotifyPropertyChanged should be implemented in ViewModel, this because this interface only informs a change.


References:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.