i have an application that use the MVVM pattern and I would like to implement validation when the user is fill the information.
I would like to use the IDataErrorInfo, but I don't know if it is a good idea that my view model implements that interface or if it is better that I create a new class. How is the best way to imlpement validantion with IDataErrorInfo and the MVVM pattern?
EDIT: I see that in some examples the implementation is in the model (it is not the same than the view model), but in my case the model basically is the POCO entities that I create from my database when I create my edmx model with entity framework, so I would like to avoid the needed to modify this entities because if I need no update my model, I would have to do the work again.
Thanks.
It is always a good idea to separate validation logic from UI. In this way, using IDataErrorInfo is correct.
Between view model and model, I prefer implementing IDataErrorInfo on view model since this interface is used by UI. You can simulate UI by calling indexers directly in your test code but if you really need validation logic in your business logic layer, such a call does not make much sense.
In our project, validation is an more independent component, which can be used by both presentation layer and business logic layer by configuration. From view model's perspective, it is very thin, containing only a call and constructing validation result inside indexer.
Also, another consideration is INotifyDataErrorInfo, which provided by .Net 4.5 and Silverlight. It provides more validation results from one property and asynchronous validation for a time consuming validation, which is what we want after we planned to update to .Net 4.5.
Hope it can help you.