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);
}
}
This should help a little: http://answall.com/q/21539/101
– Maniero
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.
– Bruno Costa
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!
– Angelo Belchior