使用 POCO 上的数据注释和 MVC 进行远程验证
我正在开发一个 ASP.NET MVC 应用程序,并且一直在研究在我的 POCO 上使用数据注释,这些注释是在我的服务层中定义的。只要我引用了 System.ComponentModel & System.ComponentModel.DataAnnotations 这没问题,我喜欢它的是它允许我在 Win Forms 应用程序中重用我的服务层。
我现在希望使用数据注释进行一些远程验证,并查看了这篇文章:http://msdn.microsoft.com/en-us/library/ff398048(VS.100).aspx
但是,要在这种情况下使用数据注释,我需要参考System.Web.MVC, System.Web 和 System.Web.Routing。这引入了对 Web DLL 的依赖, 在某种程度上限制了我。
任何人都可以推荐一种使用不与 ASP.NET MVC 耦合的数据注释来实现远程数据验证的良好干净方法,或者建议更好的替代方案。
感谢您的帮助!
I am developing an ASP.NET MVC app and I've been looking into using Data Annotations on my POCO's which are defined in my Service Layer. As long as I have a reference to System.ComponentModel & System.ComponentModel.DataAnnotations this is no problem and what I like about this is that it allows me to reuse my Service Layer in a Win Forms app.
I'm now looking to do some Remote Validation using Data Annotations and have taken a look at this article: http://msdn.microsoft.com/en-us/library/ff398048(VS.100).aspx
However, to use the Data Annotations in this context I need to reference System.Web.MVC,
System.Web and System.Web.Routing. This introduces a dependency on the Web DLL's, which
limits me somewhat.
Can anyone recommend a good clean way to implement Remote Data Validation using Data Annotations that isn't coupled with ASP.NET MVC or maybe suggest a better alternative.
Thanks for the help !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终解决了这个问题,不再将 DataAnnotations 放在 POCO 上,而是将它们放在 ViewModel 上,并使用 AutoMapper 在 ViewModel 和 POCO 之间进行映射(这节省了我进行繁琐映射的时间)。这里有一个关于 Automapper 的精彩视频 http://perseus.franklins.net/dnrtvplayer /player.aspx?ShowNum=0155
I resolved this in the end by backing away from putting DataAnnotations on my POCO's and instead I put them on my ViewModels and use AutoMapper to Map between my ViewModel and POCO (which saves me some time doing tedious mapping). There's a great video on Automapper here http://perseus.franklins.net/dnrtvplayer/player.aspx?ShowNum=0155