In MVVM, is every ViewModel coupled to just one Model?

前端 未结 7 1125
日久生厌
日久生厌 2020-12-04 17:04

In an MVVM implementation, is every ViewModel coupled to just one Model?

I am trying to implement the MVVM pattern in a project but I found

7条回答
  •  不思量自难忘°
    2020-12-04 17:29

    A ViewModel may and in many cases does use multiple Models. It is itself a "Model" of your view.

    Consider a profile screen that a user enters their personal information including address. If the address is stored in an "addresses" table and the rest in a "profile" table, then the ViewModel uses both the Profile and Address models to create a unified ViewModel.

    As jgauffin mentioned in his answer, many times you can use user controls to achieve a one to one relationship, but you can also introduce needless complexity by trying for this 100% of the time.

提交回复
热议问题