Constraint vs validation?

I am working with Windows Forms Databinding implementing interfaces like IDataErrorInfo

. For this to work, the domain (or business object) is allowed to be in an invalid state. In fact, the domain object must store an invalid value because the user entered it in order IDataErrorInfo

for it to work correctly. As long as the object is not invalidated, we are fine.

However, since the subject of the topic suggests I was wondering if there is a difference between contradiction and validation. The former would prevent the user from making changes to EVERYTHING, and later is the type of validation I described above.

Let me explain - if you have a collection Person

and Person

has a property SSN

. The tab PersonCollection

indicates SSN

what the collection means, there can be no two Persons

with the same SSN

. If we allow a temporary invalid state on Person

, we introduce a situation where there are two Persons

with a duplicate in the collection SSN

, albeit temporarily. This can lead to problems when another object is working with PersonCollection

, looking for the Person

duplicated object SSN

, getting two objects.

So, to me, it looks like some types of validations should be constraints, not (post-modified) validations.

Thoughts?

+1


source to share


1 answer


To take your example, part of the check for Person

must be a rule that checks that it's SSN

not a duplicate (by the way: in the case of duplicates, how do you know which one is right?).

If you run into a problem because yours PersonCollection

is actually IDictionary

with the SSN

s key , wait for it to Person

be checked before adding it to the collection. The object must be temporarily invalid for you to do this.



For more information on validation, check out my answer to Business Objects, Validations and Exceptions .

+1


source







All Articles