Design – MVVM with multiple view models that share a model

designdesign-patternsmvvm

I have an application where there is an "inventor" model whose data I would like to display differently in different areas of the application. Therefore, I'd like to create different view models backed by that one model.

For instance, in one section, I might display just a list of inventors with their first, middle, and last name. In another area of the application, I might display the inventors with only their initials.

A clean implementation of MVVM would seem to dictate that I'd have one model for every view model; however, if there is one notion of a model — the inventor — which contains a lot of data that is common across multiple usage areas in an application, then I feel as though there should be one concrete model that is shared by multiple view models.

I've not been able to locate anything that seems to address this, though I'm surprised because I feel like it would be a common question for beginners in MVVM. Is there a "standard" or accepted answer to this?

Best Answer

I'm afraid the premise that there's a model for each view model is wrong.

The model represents your data, and the data exists only once in your application. Suppose there were a variety of models around and you would have to keep them in sync. On what grounds would this be reasonable?

But perhaps you just confused the model with the view.
For each view you can (and imho should) have an associated view model. So, you would have 1 model, 3 view models and 3 views. The model contains the raw data, the 3 view models "process" the raw data into a format that fits the needs of their respective views.

Related Topic