Contents
Should every view have a ViewModel?
For a best mvvm pattern implementation each view must have the own viewmodel, and don’t share anythings with other. You can think the couple view and viewModel with one only object, view is presentation side than viewModel is the business side.
What is a ViewModel used for?
The ViewModel class is designed to store and manage UI-related data in a lifecycle conscious way. The ViewModel class allows data to survive configuration changes such as screen rotations. The Android framework manages the lifecycles of UI controllers, such as activities and fragments.
Why are view models special?
View models provide a convenient object to pass to a rendering engine to create an HTML page. One of many reasons to use a view model is that view models provide a way to unit test certain presentation tasks such as handling user input, validating data, retrieving data for display, etc.
Should a view and a model communicate or not?
Some say it is OK for the View to access the Models directly, other say you should wrap the Models in ViewModels in order to abstract them away from the View. I’m personally not a fan of either approach. The one of the primary goals of MVC/MVVM is to decouple the UI, business logic and data.
What is the role of the view model?
Offering data to the views is an essential role of the view-model. However, there are many different ways data can be offered while still using XAML’s data binding technology. This is the simplest and probably most common version of the model-side patterns. Here, the view-model acts as a surrogate data context.
Why do we need a view model in MVC?
A good explanation of this can be found in Dino Esposito’s The Three Models of ASP.NET MVC Apps. This sort of view-model can easily be recognized because it doesn’t have any other role or responsibility other than holding data and business rules that act solely upon that data.
How is the real data exposed in a view model?
The real data is exposed by the view-model as one or more simple properties. In a way, this is a non-pattern. It only exists so the view-model can be associated with the view’s real data context property and inject other functionality such as ICommands that wrap navigation or service calls.