IDataErrorInfo vs ValidationRule vs Exception

后端 未结 3 1787
故里飘歌
故里飘歌 2020-12-09 16:31

Can anyone tell me which is a better approach for Validation in WPF.

  1. Implementing IDataErrorInfo
  2. Creating ValidationRule
  3. Throwing Exceptions
3条回答
  •  猫巷女王i
    2020-12-09 17:10

    It is not good idea to use exception for error handling. Using exception will reduce performance. It is a matter of selecting and Implementing IDataErrorInfo or Creating ValidationRule.

    IDataErrorInfo

    • Validation logic keep in view model and easy to implement and maintain
    • Full control over all fields in the viewmodel

    Validation Rule

    • Maintains the validation rule in separate class
    • Increase re-usability. For example you can implement required field validations class reuse it throughout the application.

    My opinion is, for common validation like required field validations, email address validattions you can use validation rule. If you need to do custom validations like range validations , or whatever custom validation use IDataerrorinfo.

提交回复
热议问题