且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

DDD和放大器;客户端验证

更新时间:2022-06-03 21:58:28

我还没有遇到一个包罗万象的验证解决方案。其中一个原因是,验证逻辑可以是微妙的不同取决于应用层上。例如,不是所有的规则由域层强制执行可以执行在客户端,因此总是会有情况下,客户端验证可以通过,但你仍然需要显示它从领域层传播的验证消息。

I have not come across an all encompassing validation solution. One reason for this is that validation logic can be subtly different depending on the application layer. For example, not all rules enforced by the domain layer can be enforced on the client side and so there will always be cases where client side validation may pass and yet you still need to display a validation message which propagated from the domain layer.

不过,在ASP.NET MVC中的验证模式是可扩展的,你可以扩展它以支持额外的验证规则或事件验证框架比其他的DataAnnotations。 这里是与ASP.NET MVC集成企业库验证块的例子,但正如文章所指出的,客户端验证没有得到执行。另一种方法是使用在你的领域层的DataAnnotations属性。该命名空间的DataAnnotations是不依赖于ASP.NET MVC。

However, the validation model in ASP.NET MVC is extensible and you can extend it to support additional validation rules or event a validation framework other than DataAnnotations. Here is an example of integrating Enterprise Library Validation block with ASP.NET MVC, however as the article points out, client side validation was not implemented. Another approach would be to use DataAnnotations attributes in your domain layer. The DataAnnotations namespace is not tied to ASP.NET MVC.

在这些方法中所面临的挑战则在于,从传播领域对象验证规则来查看模型。从理论上讲,你可以扩展 AutoMapper 使得从一个域模型验证规则结转到视图模型类,但是实施和维护的成本可能超过这个解决方案的优势

The challenge in these approaches however is that of propagating validation rules from domain objects to view models. In theory, you can extend AutoMapper such that validation rules from a domain model are carried over to view model classes, however the cost of implementation and maintenance may outweigh the benefits of this solution.

借助流利的验证框架可以作为一个起点,一个包罗万象的验证解决方案。有许多示例使用这个框架与 ASP.NET MVC。

The Fluent Validation framework could be used as a starting point for an all encompassing validation solution. There are many examples of using this framework with ASP.NET MVC.