views:

410

answers:

3

Hello,

I have plain POCO's here and as INotifyPropertyChanged is a Interface for the View's need its implemented in the ViewModel not the Model.

Now I want to show validation errors in the View beside every textbox the user typed in data.

I do not want to implemented the IDataErrorInfo interface in my Models because lets assume I am not allowed to touch them as they come from another Service/Supplier.

I do not want to put my IsCustomerFirstNameLenthValid Method into the Model because I could not have access to it or I just dont want to pollute my Models with interface`s having nothing to do there!

How can I validate my naked POCO`s in the ViewModel and forward the results to the View by showing validation errors ?

+1  A: 

This isn't the answer you are directly looking for, but in order to keep separation of concerns, I would not use your domain pocos to make up the viewmodel. I would map the domain objects to completely separate viewmodel objects. This way, all of the view concerns/plumbing can stay on the viewmodel.

Daniel Auger
+1  A: 

It sounds like what you are calling your naked POCOs are the ViewModels and that you do have access to them.

If so, implement IDataErrorInfo on them (using any support that you do get from the model object you don't have control over). Putting validation in each property setter is a (less attractive) option, as is subclassing ValidationRule.

Here is a link to a wonderful (despite being dated) article by Paul Stovell that gives a good overview of how to architect validation for a WPF app.

HTH,
Berryl

Berryl
@Berryl No I do not call my plain POCO`s ViewModels. A POCO is a Domain objectl and a ViewModel is the Model of a View.Your link to Paul Stovell also uses IDataErrorInfo for the Domain objects what I do not want see my question.@Danielhm do not not really understand you. Some language problem... ...to make up the viewmodel? My domain Pocos are not duplicated in the ViewModel they are delegated to quote Josh Smith but this is/was not my problem. I an just interested in validating my POCO`s without implementing IDataErrorInfo into them! A start I found =>
msfanboy
http://www.lostechies.com/blogs/jimmy_bogard/archive/2007/10/24/entity-validation-with-visitors-and-extension-methods.aspx
msfanboy
IF you are using an MVVM type of pattern with INotifyPropertyChanged you are, or should be, trying to take advantage of databinding technology. IDataErrorInfo is one of three basic validation schemes supported by WPF, and I listed the other two, as does Paul Stovell in his article.
Berryl
POCOs don't inherently imply either a domain object or a ViewModel; how they are used does. Since you said the domain objects were "lets assume I am not allowed to touch them", that leads me to ask just what POCOs you are able to touch. ANYWAY, Bogard's article does look interesting and that is a nice find. Cheers
Berryl
A: 

I would suggest doing your validation in the viewmodel instead of the model. This will allow you to easily implement IDateErrorInfo or other mechanisms to do your validation. Once the validation passes, you can synchonise with your POCO model objects.

There is another option if you absolutely must do validation directly on POCO model objects rather than your viewmodel though. You can use the Enterprise Library Validation Application Block (VAB) and set validation rules through configuration rather than attributes which would pollute your POCO objects. The title of your post mentions WPF rather than Silverlight, so you are in luck because you can use the Bennedik validation control to handle the display of validation errors in the UI for you automatically.

Bermo
quote:"...There is another option if you absolutely must do validation directly on POCO model objects rather than your viewmodel though..."BUT I said I do not want validation on my POCO...To clarify: POCO = domain model/class = EntityI do not want to call this in my CustomerViewModel:_customer.IsValid(); // that way I have to put validation into my POCOs what I do not want!I rather want a static method likebool IsCustomerValid(Customer customer){ // do validation}
msfanboy
Simple, call IsCustomerValid(..) from your viewmodel then. You'll need to expose similar properties to your model in your viewmodel as others have suggested so WPF validation will work. There are a few ways validation can work as Berryl has suggested in the link from his answer. Note, the IErrorDataInfo interface can work either on a model or viewmodel.
Bermo