如何在桌面应用程序中使用 DataAnnotations
当我们通过 MVC
使用 EF
(fe) 时,我们可以使用 ModelState.IsValid
来检测 model
是否可以是否传递 DataAnnotations 元数据。但是如何在桌面(win-forms / wpf)应用程序中使用 DataAnnotations 元数据?
更多:
事实上,我想创建一个与ModelState
相同的对象(一个可以保存与每个属性和消息相关的属性和消息的字典)。然后,用validation-layer
包装DAL
,在VL
中使用元数据来验证模型,即VL
可以在任何项目中使用。我知道我应该使用反光板,但我没有任何经验。感谢任何帮助和建议。
When we are using EF
(f.e.) via MVC
, we can use ModelState.IsValid
to detect a model
can pass DataAnnotations
metadata or not. But how can I use DataAnnotations
metadata in a desktop (win-forms / wpf) application?
More:
In fact, I want to create an object same as ModelState
(a dictionary that can save properties and messages associated with each). Then, wrap the DAL
by a validation-layer
, in VL
use metadata to validate models, that the VL
can be used in any project. I know I should use reflector, but I haven't any experience. Thanks to any help and suggestion.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用 EF 4.1/4.1 dbcontext,它有一个内置的验证 API,可以检查数据注释规则以及 IValidatableObject.Validate。我不太确定我是否理解您的目标,但如果是在数据层中进行验证,那么您可以使用内置的内容。(这是概述http://msdn.microsoft.com/en-us/data/gg193959)。
如果您想要自己的验证器与数据层分离,请查看 System.ComponentModel.DataAnnotations 命名空间,以获取可用来进行自己的验证的方法和其他逻辑。
If you are using EF 4.1/4.1 dbcontext, it has a built in validation API that can check Data Annotation rules as well as IValidatableObject.Validate. I'm not quite sure that I understand your goal, but if it is to have the validation in the data layer then you can just use what's built in. (Here's an overview http://msdn.microsoft.com/en-us/data/gg193959).
If you want your own validator that is separate from the data layer, then look at the System.ComponentModel.DataAnnotations namespace for methods and other logic you can leverage to do your own validation.