简体   繁体   中英

Can Models communicate with each other in MVVM

Can a Model communicate with other Models, for my case, can a Model have a list of an other Model, Example :

I have a Model called Graph, and a Model called Node.

Is it appropriate in the MVVM pattern to make the Graph class which is a Model, contain as a property, a list of the Model Node ?

Yes.

MVVM is simply a separation of concerns in terms of views and data. Views can have other views and models can encapsulate other models. The view model is the hybrid where data is stored and acquired using the models and consumed by the views.

Although the answer might be yes you do have to consider the 'proper' responsibilities in MVVM. As long as the Model maintains integrity of the model (references, domain/validation checks) it is perfectly fine to have the model classes 'communicate' with each other. However, in general, I prefer the ViewModel and the repositories to be responsible for retrieving data and translating it into ViewModel classes.

Many times I do not need any logic in the model, the model is the structure I receive from and send to the data store (file, db, web service, ...) The model is 'just' data to me and preferably generated (proxy generation, Entity Framework, ...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM