In MVVM can a Viewmodel know more than one model?

Asked

Viewed 165 times

3

In the MVVM architecture, a viewmodel can "meet" several viewmodels. What about the VM-M relationship? A viewmodel can "meet" several models different?

Ex:

CreditCardViewModel

--> Model CreditCard

--> Model BusinessInfo

--> Model UserRegister

Or each viewmodel should have your responsibility and I have to call her method when I want a specific result?

1 answer

3


The doubt is legitimate, but I wonder how it came about. Is it because people make so many rules that there’s always a question if there’s a rule for it? What I found most curious is to know a part of the answer.

I am against rules, I am in favor of understanding what you are doing, there may even arise a personal rule, with its due exceptions if it is pertinent (it is not always pertinent to have even personal rules). So why should there be a rule that says this? This is important to know and can give the answer.

We have already given some answers on the subject:

We can see that the reason that this element exists in the construction of software is precisely to join several models. Not only that, it can be taking parts of a model, or it can take only one model. But it can be almost slutty if you take a model and play it on viewmodel, not that it cannot or should not, it has to do by standardization, but it is a code that does nothing useful, and if practically all its models are equal to the models of vision then maybe its application should not have adopted it except by obligation of adopted architecture (which shows that it was a wrong adoption, although in some cases there is no escape from it).

Now, whether you’re going to do it right is another matter. Doesn’t mean that all viewmodel should know several models. What makes sense to be so will be defined depending on what you are solving. So you need to understand what the viewmodel, If what you want to do meets well what it proposes then it can and even should do, if it makes sense in that. Put several models in a viewmodel without needing it is wrong, and this should be obvious. If it is possible to put several models in the viewmodel I ask myself, why not put, if it makes sense to the problem? And what would be the solution then? The questions are rhetorical, is that I do not see how it could be different, after knowing what is a VM.

  • Thank you very much for your reply. In fact, it was an external decision that led me to ask about the grounds that justified this decision-making, other than the division of responsibility of each viewmodel, as if the fact that it uses various resources would prevent it from exercising its function! Quite the contrary, it helps... As I wondered if this made sense or not, I decided to ask the question. But as they say, it sends who can, questions who has discernment, and obeys when the situation is irreducible... Let me put out some fires here.

  • Yeah, you could tell something like that in the question. You have some allowances to try to argue again, and you can find others by following what you have now obtained.

  • And I think the question leaves on the air the tone of "I can or I can’t "when in fact your answer answers very well. Do it but if you really don’t have another way to do it! Great reply @Maniero.

  • @Ricardolucas Actually I think this should be easy to decide by understanding what a viewmodel, I’m surprised some people have doubts about this (considering that she decides to use a VM knowing what it is, and if she adopted without knowing it then everything is already wrong and nothing else matters), even worse is the person knowing what is a viewmodel and decide that she can’t have multiple models. Apparently one only does CRUD extremely simple.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.