Best Practices for IDataErrorInfo

I am working in a WPF project using MVVM .

What is the best practice for checking errors with IDataErrorInfo ? In my model or ViewModel?

What is the best template for implementing validation?

PS I am using .NET 3.5.

+4
source share
2 answers

I think there is no right way or wrong way. It all depends on your application and whether you use different templates or architectures or have specific needs for your WPF application.

If you use a different layered architecture, you can put your validation in the business layer of your application. In this case, use this link .

In my applications, I like to set validation in the viewmodel. Obviously, in some cases this is, for example, a bad idea; If you have the FirstName property in your view model, this means that you are only restricting the GUI for checking the FirstName property, but what if someone sets it from another location.

It all comes down to the needs of your application and requirements. Personally speaking, I put them in my ViewModel as quick and easy. But for best practice, I recommend you read the link.

I also recommend that you read the following links to give you a better understanding;

How to use model validation rules in WPF ViewModel

OR

Business Level Validation Example

Hope this helps!

+5
source

If you bind your model to a view, it would be better to use data annotations and the Validator class. If you bind to the properties of your view model, use the IDataErrorInfo identifier to verify.

0
source

Source: https://habr.com/ru/post/1480723/


All Articles