Validating domain objects
The following is a good explanation from Greg Young in an online discussion: Let's propose we now have a Send User Creation Email Service that takes a User Profile ... you just don't bother to check and "hope for the best"—you hope that someone bothered to validate it before sending it to you.
how can we rationalize in that service that Name is not null? Of course, using TDD one of the first tests we should be writing is that if I send a customer with a null name that it should raise an error.
Command validation happens after the form is submitted and before the Command is passed to the model.
In practice, they end up coupling everything to everything.Let's assume we have ENUM field with only a couple values: Safe from what?You're speaking of objects but objects are just an intermediate representation of your data, and you should treat them this way, because it allows you to avoid an inevitable collapse of complexity when you add more and more responsibility to your class making reasoning about things very, very hard. I'd use a type for enumeration because the type is a separate entity which I can pass throughout the application and use it for validation, coercion, maybe some source for metaprogramming.Invariant rules are simply expressed as contracts, and exceptions or notifications are raised when they are violated.
The reasoning behind this is that many bugs occur because objects are in a state they should never have been in.You can validate multiple property values to infer business constraints within the object.