What is the difference between Invariants and Validation Rules?

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

Or are they different from where the Invariants should protect the object from invalidity, and the verification consists in checking the validity of the object even after it has changed its state (it may be in a valid or invalid state)? In the above example, if I use invariants, I check the invariant before updating the discount, and in the case of verification, I apply a 50% discount, and then check for validity (the object is already in an invalid state).

+10
source share
2 answers

Absolutely, validation is the process of approving a given state of an object, while invariant enforcement occurs before that state is already reached.

The corollary is that invariant enforcement is best performed by a thing that itself mutates (or is created) like a self-defense reflex, while verification is usually performed by a third party.

The always-functioning school of thought advocates the use of invariants over validation. I think it is great for DDD and Aggregates.

+14
source

Yeah i think so

In DDD, validation rules can be considered invariants. The main responsibility of the unit is to use invariants when changing state for all objects in this unit.

You can refer more information on this page.

0
source

Source: https://habr.com/ru/post/987042/


All Articles