不变式和验证规则有什么区别? [英] What is the difference between Invariants and Validation Rules?

查看:130
本文介绍了不变式和验证规则有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常在DDD中看到术语不变式. 此处 Dino Esposito谈到了这一点.如果查看.NET库,则会看到 ValidationAttribute 类. 不变式验证规则是否相同?例如,我能否说只有订单总额超过$ 250的50%折扣是可用的是不变的?

I often see the term Invariants in DDD. Here Dino Esposito talks about it. If I look at the .NET library, I see a ValidationAttribute class. Are Invariants and validation rules the same? For example, can I say 50% discount is available only if the order total is more than $250 is an Invariant?

还是不变的,即不变式是为了保护对象免受无效影响,而验证是即使对象更改了状态(它可以处于有效或无效状态),也要检查对象的有效性?在上面的示例中,如果我使用不变量,则在更新折扣之前检查不变量,在进行验证的情况下,我应用50%的折扣,然后检查有效性(对象已经处于无效状态)./p>

Or are they different where Invariants are to protect an object from becoming invalid and validation is to check the validity of the object even after it has changed it's state (it can be in a valid or invalid state)? In the above example, if I use invariants, I check for the invariant before updating the discount and in the case of validation, I apply the 50% discount and then check for the validity (the object is already is in invalid state).

推荐答案

当然,验证是批准给定对象状态的过程,而不变的强制执行甚至要在达到该状态之前进行.

Absolutely, validation is the process of approving a given object state, while invariant enforcement happens before that state has even been reached.

一个推论是,不变的执法最好是通过自己变异(或创造)的事物来执行的,例如自我保护反射,而验证通常是由第三方完成的.

A corollary is that invariant enforcement is best performed by the thing that is being mutated (or created) itself, like a self-protection reflex, whereas validation is usually done by a third party.

始终有效的思想流派主张使用不变量过度验证.我认为它与DDD和聚合非常吻合.

The Always valid school of thought advocates the use of invariants over validation. I think it goes perfectly with DDD and Aggregates.

这篇关于不变式和验证规则有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆