Should the MVVM ViewModel perform type conversion/validation?

后端 未结 5 1011
闹比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 08:58

    Or should ViewModel properties expose the actual data types, leaving such chores to the view to handle?

    1. Conversion and templates are done in View, because they both are just a conversion of values, models and viewmodels into controls! Controls are available only inside View.

    2. Validation is done in ViewModel, because validation is done according to business rules and can even be done through a call to a remote service. View knows nothing about business rules, but knows how to present validation results.

    If, say, a non-numeric value is entered in a text box bound to a numeric property

    A properly crafted numeric text box control never allows user to input a non-numeric value.

提交回复
热议问题