Should the MVVM ViewModel perform type conversion/validation?

后端 未结 5 1066
闹比i
闹比i 2021-01-31 08:19

We\'re just getting into MVVM in WPF.

We have implemented our ViewModels with \'strongly typed\' properties (int, double? etc.) that we bind to in the view.

Type

5条回答
  •  甜味超标
    2021-01-31 09:09

    Absolutely it belongs in the view model, for all the usual reasons, including:

    • Designers own the XAML. Do you want the designers to have to understand and implement the requisite type conversion and validation logic?
    • Testability. Don't you want to validate that your conversion and validation logic is working correctly? It's much harder if it's embedded in the view.

    On the other hand, conversion, validation etc. will be less declarative, explicit and flexible, from the point of view of the View designer

    I think this is a moot point because the view designer should be responsible for these things. The designer is trying to make the UI look and feel a certain way; it is the developer who implements the business logic, including conversion and validation logic.

提交回复
热议问题