Are current MVVM view model practices a violation of the Single Responsibility Principle?

后端 未结 4 1019
天涯浪人
天涯浪人 2021-02-01 20:02

With current practices (at least with WPF and Silverlight) we see views bound via command bindings in the view model or we at least see view events handled in view models. This

4条回答
  •  南笙
    南笙 (楼主)
    2021-02-01 20:41

    Single Responsibility as Martin defines it:

    "THERE SHOULD NEVER BE MORE THAN ONE REASON FOR A CLASS TO CHANGE."

    A ViewModel, as far as MVVM is concerned is really just a specialized implementation of a Presentation Model.

    So while it could be argued that a Presentation Model should only represent the state of the UI, and that a Presenter/Controller should always broker the commands between the UI and the Presentation Model. If one follows this idea, with SRP dividing on State and Commands, then adding a command should not affect the class that represents state. Therefore MVVM would break SRP.

    However...

    I think this is grasping at straws. MVVM is a fairly specialized implementation used basically in WPF/Silverlight (and now browser clients).

    Patterns are designed to make designs simpler where the alternative would be more cumbersome or less maintainable. Since MVVM is designed to take advantage of the extremely rich data binding capabilities of the presentation technologies, then it is a worthwhile trade off.

提交回复
热议问题