Understanding application flow with MVVM

Viewed 214

I am having a hard time understanding how MVVM is used in case of more complex applications. All the examples I can find are extremely basic Apps.

Let's say I have a "Package Delivery" application. To perform a delivery I have to do 3 steps:

  1. Scan the package
  2. Enter any damages or problems with the package
  3. Let the recipient sign on the device

All this information gets validated on the device and then sent to the backend.

In MVC I would implement this like this:

MVC Implementation

The DeliveryController handles all of the logic. This includes navigation between pages, fetching of API data and validating all of the data once it is all collected.

The controller acts as the "Connection" between the Views and collects all the information and brings it together.

But how would this be done in MVVM? Where would all the data be brought together? Most implementations of MVVM I can find do something like this:

enter image description here

In this, the data entered in each View would have to be passed to the next ViewModel until the end of the chain is reached. At that point the SignatureViewModel would do the validation and make the API call. That seems very weird and like it would get very confusing, since data would just be "passed through" multiple ViewModels just to have it at the end of the chain.

Another option I see would be that each ViewModel handles it's own data:

enter image description here

Here for instance the DamagesViewModel would validate and send the data it's own View handles. The big issue with this is that data does not get sent as a whole. Also there can not be any validation of the entire data before it is sent.

My last idea would look like this:

enter image description here

This adds a DeliveryViewModel that essentials acts like the DeliveryController does in MVC. It would handle which ViewModel to navigate to next, handle what API calls to make and validate all the data once it is entered.

To me (as someone who has mostly used MVC) this last options seems most sensible. But I also feel like it might miss the point of MVVM.

How is this usually done in MVVM? I would really appreciate any pointers. Links to articles that explain this well are much appreciated.

Also if anyone knows of any publicly available repositories or projects that have this kind of pattern in them, I would love to see them.

3 Answers

I have struggled quite a bit with the same thing. Especially since I’ve used MVC quite a lot.

I think the most important thing to consider is that MVVM is not meant to solve exactly the same things as MVC. You can still implement a controller outside of this pattern to complement it. It also changes quite a lot the general design of the application.

One mean to do it that I have used is to implement the controller as a singleton that can be shared between VewModels. This controller can per example be injected into the ViewModels using dependency injection. Viewmodels could then for exemple subscribe to events coming from the controller to update. But this is one way to solve this problem among others.

Even when using MVVM I still use some form of a controller and consider ViewModels as basically transformation of data to facilitate the view.
So for me there is still a service or controller layer that completely separates the middle and back end tier from the rest of the architecture. View models come into play depending on the consumer/app - fetches data from the service tier, transforming the data, validating etc for the purpose of a consumer/app/etc.

Related