How to build the ViewModel in MVVM not to violate the Single Responsibility Principle?

后端 未结 4 1025
花落未央
花落未央 2021-01-17 12:15

Robert Martin says: \"There should never be more than one reason for a class to change\".

Let\'s consider the ViewModel class which is bound to a View. It is

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-17 12:55

    The ViewModel single responsibility is to provide the View the information it needs. If the View needs different and unrelated properties that is not important as the ViewModel only has one reason to change and that is the View showing different properties. So you shouldn't worry too much.

    That said, if the ViewModel does get huge, maybe you could think about dividing the view into several subviews to improve reusability and keep the Views and the ViewModels manageable.

提交回复
热议问题