ASP.NET MVC:如何在服务层执行数据注释验证?
在最近提出的一个问题中: ASP.NET MVC:数据注释验证就足够了吗?
。 ..得出的结论是,依靠数据注释验证(由模型绑定器触发)不足以确保验证始终执行。我们仍然需要在服务层(或者 ModelBinding 发生后的其他地方)添加相同的验证逻辑。不幸的是,我们将重复我们的验证代码(一次使用数据注释,再次在服务层)。服务层是否有一种简单的方法可以根据数据注释中定义的内容触发验证?如果这是可能的,那么我们将两全其美……我们不需要重复验证代码,但我们仍然会确保验证始终被执行。
In a recent question posed here:
ASP.NET MVC: Is Data Annotation Validation Enough?
...it was concluded that relying on data annotation validation (triggered by the model binder) was not enough to ensure that validation was always executed. We still need to add the same validation logic in the services layer (or somewhere else after ModelBinding happens). The unfortunately thing about this is that we will be duplicating our validation code (once with Data Annotations and again in the services layer). Is there an easy way for the services layer to trigger validation based on what's been defined in Data Annotations? If this can be possible, then we will get the best of both worlds...we won't need to repeat the validation code, but we'll still ensure that the validation always gets executed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
借助此博客: http://goneale.com/2009/03/04/using-metadatatype-attribute-with-aspnet-mvc-xval-validation-framework/
我能够创建一个方法,根据数据注释定义的验证来测试我的对象。它将执行从 ValidateAttribute 派生的任何验证属性。我现在可以从我的服务层(或 DomainModel)将我的对象传递给此方法,并且我的服务层不再依赖于控制器。这将确保在将数据保存到数据库之前始终执行验证。我无法按原样使用博客上的代码,因为我似乎无法访问 Graham 使用的某些扩展方法,所以这是我的版本:
此代码适用于执行和不执行的两个类没有伙伴类,尽管如果您不使用伙伴类,则可以稍微简化此代码。
我希望你觉得这很有用。
With the help of this blog: http://goneale.com/2009/03/04/using-metadatatype-attribute-with-aspnet-mvc-xval-validation-framework/
I was able to create a method that will test my object based on validations defined by data annotations. It will execute any validation attribute that derives from ValidateAttribute. I can now pass my object to this method from my service layer (or DomainModel) and my service layer is no longer dependent on the controller. This will ensure that validation will always be executed prior to persisting data into the database. I couldn't use the code on the blog as is, as I don't seem to have access to some of the extension methods that Graham was using, so here's my version of it:
This code works with both classes that do and don't have buddy classes, although if you don't use buddy classes, this code can be simplified a bit.
I hope you find this useful.
您没有检查过我的回答你之前的问题?
我提交了一些基于 DTO 的 DataAnnotation 属性进行自动验证的代码。只要您的 DTO 在控制器操作的参数中使用,它们就会被此属性拾取并验证,无论如何。
唯一的问题是:如何生成 DTO?
如果您可以控制 DTO 类的生成,那么您也可以向它们添加额外的接口。我发布的代码使用 T4 over EF、xVal 和 DataAnnotation 以及声明在每个实体类中实现的 Validate() 方法的自定义接口。
Haven't you checked my answer in your previous question?
I submitted some code that does automatic validation based on DataAnnotation attributes of DTOs. As long as your DTOs are used in your Controller Action's parameters, they will get picked up by this attribute and validated no matter what.
The only question is: how do you generate your DTOs?
If you can control your DTO class generation, then you may as well add additiona interface to them. The code that I posted uses T4 over EF, xVal and DataAnnotation and custom interface that declares
Validate()
method that is implemented in each entity class.我想实现同样的目标并尝试了约翰尼的答案。它可以正常工作,直到您进行与其他属性相关的验证(例如使用RequiredIf 属性)。
我最终在 System.ComponentModel.DataAnnotations 中使用了 Validator 类,它实际上是为此目的,并且应该应用与通常应用的完全相同的完整逻辑。
这是向您展示如何使用 Validator 类执行此操作的示例方法。
I wanted to achieve the same and tried Johnny's answer. It works fine until you have validations that are relative to other properties, like using the RequiredIf attribute.
I ended up with using the Validator class in System.ComponentModel.DataAnnotations which is actually ment for this and should apply the exact same full logic that's normally applied.
This is the example method that shows you how to do this with the Validator class.