MVC 验证和业务层
DataAnnotations 验证发生在默认模型绑定器中,我见过的大多数示例都使用控制器中的 Model.IsValid 来验证模型是否有效。由于我的控制器操作调用业务层方法,并且我喜欢验证那里的实体:
- 我是否必须显式关闭 模型绑定器验证?
- 如何验证中的实体 业务层。换句话说,如何 我是否会触发验证 目的?
- 另外,我正在使用视图模型。我是吗 将验证属性添加到 查看模型?如果是这样,因为查看模型 与 UI 绑定在一起,怎么样 业务层验证??
The DataAnnotations validation happens in the default model binder and most of the examples I've seen uses the Model.IsValid
in the Controller to verify if a model is valid or not. Since my controller action calls a business layer method and I like to validate the entity there:
- Do I have to explicitly switch off
the model binder validation? - How do I validate the entity in the
business layer. In other words, how
do I trigger validation given an
object? - Also, I am using View Models. Do I
add the validation attributes to the
view model? If so, since View models
are being tied to the UI, what about
validation at the business layer??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将首先回答您的问题#3:是的,在使用视图模型时,请在视图模型的属性上添加数据注释验证属性。正如您所指出的,视图模型与 UI 相关联,因此它们存在表示问题,并且验证严格针对 UI 输入验证。您在此处应用的验证属性将由框架自动调用,您可以在控制器中检查 ModelState.IsValid (您也指出了这一点)。
关于验证业务层中的对象,有多种方法可以实现这一点。例如,您还可以在业务层域模型实体上使用数据注释。您还可以使用其他框架,例如 Enterprise Library 验证应用程序块、Fluent Validation 等。但在这种情况下,您可能会进行显式调用来验证您的域对象(并且每个框架都有自己的机制来执行此操作)。根据上面的描述,我假设您的视图模型和域模型之间存在映射(可能使用 AutoMapper 之类的东西)。
话虽如此,关于您的问题#1,我不会关闭模型绑定程序验证。让其正常执行视图模型上的验证。将视图模型映射到域模型类。然后随意为您的域模型执行额外的业务对象验证层。您甚至可能没有在 MVC 项目中进行此验证 - 这可能封装在您应用程序中其他位置的业务层中。
I'm gonna start by answering your question #3: yes, when using view models, add Data Annotation validation attributes right on the properties on your view model. As you pointed out, the view models are tied to the UI so they have presentation concerns and the validation is strictly for UI input validation. The validation attributes you apply here will be automatically invoked by the framework and you can check ModelState.IsValid in your controller (which you also pointed out).
In reference to validating objects in your business layer, there are numerous ways to do this. For example you could also use data annotations on your business layer domain model entities as well. You could also use other frameworks like Enterprise Library validation application block, Fluent Validation, etc. But in this case, you're probably going to be making an explicit call to validate your domain objects (and each of these frameworks has their own mechanism for doing so). I'm presuming your mapping between your view models and domain models (probably with something like AutoMapper) given your description above.
Having said all that, in reference to your question #1, I would not switch off model binder validation. Let that performance the validation on your view models as normal. Map your view models to your domain model classes. Then feel free to perform an additional layer of business object validation for your domain model. You may not even doing this validation in the MVC project - this might be encapsulated in a business layer that you have somewhere else in your app.