开发者

Following Domain Driven Design with MVVM/WPF

开发者 https://www.devze.com 2022-12-23 19:56 出处:网络
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.

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 ?


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号