Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Responsibility of a ViewModel

I am building a tool which goal is to download a file based on a few parameters.

First step is to set (or retrieve) those parameters.

The different parameters sets are retrieved (let's say via configuration files) by a FileDownloadsManager : it knows exactly which parameters to use in order to download the right file.

those parameters are stored in a class, and I have a list of instances of this class.

That means that I can download my file with multiple possible parameters sets.

Around those ParameterSets, I've built ParametersSetsViewModels so that I can display them in a list, and add some View-Only properties. Internally, the ParametersSetsViewModels have a reference to the underlying ParametersSets used as a source for the members of the View Model.

now, when I select my parameters set, I would like the related file to be downloaded.

Whose responsibility should this be ?

I have this feeling that if the ViewModel is too active, by having a method that returns the downloaded file, this would be against the MVVM pattern; what is your take on this ?

Bonus : the download should be feasible in the background with BackgroundWorkers or WebClient's asynchronous methods.

like image 357
Fabio Salvalai Avatar asked Aug 16 '10 22:08

Fabio Salvalai


People also ask

What are the responsibilities of a ViewModel?

ViewModel is a class that is responsible for preparing and managing the data for an Activity or a Fragment . It also handles the communication of the Activity / Fragment with the rest of the application (e.g. calling the business logic classes).

What is the purpose of ViewModel in MVVM?

The viewmodel of MVVM is a value converter, meaning the viewmodel is responsible for exposing (converting) the data objects from the model in such a way that objects are easily managed and presented. In this respect, the viewmodel is more model than view, and handles most if not all of the view's display logic.

How does a ViewModel work internally?

ViewModel class is designed to store and manage UI-related data in a lifecycle conscious way.It is the main component in the MVVM architecture. ViewModel can be created with activity context or fragment context. When a ViewModel object is created, it is stored inside Activity OR FragmentManager.

How do you define a ViewModel?

A view model represents the data that you want to display on your view/page, whether it be used for static text or for input values (like textboxes and dropdown lists) that can be added to the database (or edited). It is something different than your domain model . It is a model for the view.


1 Answers

It seems to me that everyone assumes MVVM has no controllers as they left out the C. MVVM is actually a variation of MVC "that just adds ViewModels".

Maybe it should have been called MVCVM instead?

ViewModels are only there to offload the "GUI" code from the view and to contain any data for binding. ViewModels should not do any processing. A good test is that your ViewModel is testable via automated unit tests and has no dependencies on data sources etc. They should have no idea where the data is actually coming from (or who is displaying it).

Although it can be overlooked/avoided, a Controller is responsible for deciding what data model to display and in which views. The ViewModel is a bridge between Models (the M in MVVM) and Views. This allows simpler "separated" XAML authoring.

In answer to your question the processing should be handled by a controller. If it needs to update the ViewModel to show busy indicators etc that is fine, but it is not the View or the Model or the ViewModel's responsibility.

like image 94
Gone Coding Avatar answered Oct 08 '22 06:10

Gone Coding