Validating the MVVM model and data binding?

When searching for guides on the best methods of checking properties in WPF MVVM, I see that many people use the interface. IDataErrorInfoI just wonder if it is possible to configure automatic verification, as in ASP.Net MVC using attributes?

Can anyone say what works best for model validation in MVVM? Should the check be on the base class of the model? or in a view model class?

+3
source share
4 answers

Silverlight has a control called DataForm that works using the DataAnnotations attributes, and someone was kind enough for the port that controls WPF . I think this is something similar to what you are looking for.

+3
source

These are good questions! Validation is owned by the BOTH Model and ViewModel (s). This is how I usually approach him:

First, I put the greatest possible check in the Model - these will be rules that are not dependent on this presentation. For example, suppose an employee in your domain is invalid if it does not have an EmployeeNumber property that is not null and six characters in length, and each of the six characters must be a digit.

-, ViewModel, IDataErrorInfo. , , , ( IDataErrorInfo). IDataError info , ...

, , , . () , , , , , . , , FullName component/valueObject Employee, , , ViewModel , , .

, Validator - NHibernateValidator, , , () . , NHibernate, , . , ( MyDomainImpl). .

, . , , - .

,
Berryl

+2

, ViewModel, , :

  • ViewModel. ViewModel .
  • , , . ViewModel .

Dependency Injection , Unity, Windsor Castle .., ViewModels. ViewModel.

- http://www.castleproject.org/container/documentation/trunk/usersguide/interceptors.html

+1

?

Software systems basically need some kind of validation so that business logic only has to deal with the correct data. These validation rules are determined by the business model, and therefore the domain level is the right place to implement them. Just keep in mind that you are not starting to duplicate the verification code between business objects.

link

.

You might be interested in the WPF Application Framework (WAF) sample applications . They show how to use .NET DataAnnotations validation attributes with the MVVM pattern.

+1
source

All Articles