What is the difference between Invariants and Validation Rules?

后端 未结 2 851
面向向阳花
面向向阳花 2021-02-01 19:53

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 Invaria

2条回答
  •  情书的邮戳
    2021-02-01 20:26

    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.

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

提交回复
热议问题