3

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?

  • 7
    "A clean implementation of MVVM would seem to dictate that I'd have one model for every view model;" - Do you have a source for that? I don't see anything wrong with your approach, quite the opposite to be honest. – metacircle Sep 1 '15 at 12:36
  • Thank you for the comment. No, I don't have a specific source for that. – rory.ap Sep 1 '15 at 13:20
  • 2
    We have one model for dozens of viewmodels. Basically, this is not a problem. – MetalMikester Sep 1 '15 at 15:07
  • Wonderful. That is the approach I've chosen. I am basically just looking for validation from the community, which the two of you have provided. Thanks. – rory.ap Sep 1 '15 at 15:10
  • 1
    "A clean implementation of MVVM would seem to dictate that I'd have one model for every view model;" - I honestly don't think so. Just like there can be several Views using the same view model, for example different layouts for different devices for the same interaction with data there can be several different view models for a single model, providing different interactions with the said data. Also, some view models might use more then one model. In your example it might show the inventor as well as all of his inventions. – Morothar Sep 2 '15 at 8:35
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.