1

I am working on a WPF application using MVVM pattern. I need to do data validation(using Data Annotations) for my data entry screens.

But the tricky part to write a common piece of code is rather than using simple property I need to use properties with are calling GetValue and SetValue method. But my entities are being generated by entity framework using templates and modifying template to achieve this things seems very difficult.

I am using this technique to validate my data

http://blogs.microsoft.co.il/blogs/tomershamam/archive/2010/10/28/wpf-data-validation-using-net-data-annotations-part-ii.aspx

http://blogs.microsoft.co.il/blogs/tomershamam/archive/2010/10/11/notifyingobject-for-wpf-amp-silverlight.aspx

If there is better and authentic way to validate the entity data, i'll be more than happy to change my approach or if I need to change the template then please point me to some great reference.

4

1 回答 1

1

首先。这样做真的很糟糕,不是因为它很难,而是因为你需要继承DependencyObject. DependencyObject是一个 STA 线程类。您将无法使用工作线程来实例化您的对象,这意味着您不能使用工作线程来使用实体框架。

现在,我建议您将 MVVM 模式与 WPF 一起使用。您应该将 View 绑定到 ViewModel,然后将其绑定到您的 Entity Framework POCO 对象。

您可以尝试IValidateObject为您验证https://stackoverflow.com/questions/13917890/mvc4-custom-validation-via-ivalidateobject-validate-method-not-working

然而,我个人更喜欢 FluentValidation 库。

于 2013-08-26T17:52:14.480 回答