Otis wrote:
There are no 'good' solutions. IDataErrorInfo is a stupid interface, we can't change that. You have total freedom over what it will provide, though we can't change the nature of the interface.
I do agree that IDataErrorInfo is a stupid interface, and that you can't change it. However, I must say that we don't have total freedom of what to do with it, as you are clearing the error info for us. I would recommend to remove the clearing from SetValue(), however there might be customers whom rely on you clearing the error info. Their validation code would look like:
if (!isValidLastName){
involvedEntity.SetEntityFieldError(CustomerFields.LastName.Name, "Name is not valid"), False);
}
They return isValid and the validation framework "restores" or "updates" the value occordingly. Ofcourse, I do not have any objection in doing this: I think the validation framework design is pretty solid here.
However, if we would like to always return true (because we didn't implement ValidateFieldValue() or because we would like entities to allow invalid values (I do reckon this is not as you've designed it, but still)), we don't have the option to set the error info, as you always clear it!
If you removed the clearing of the error info, the code I would write is:
involvedEntity.SetEntityFieldError(CustomerFields.LastName.Name, (isValidLastName ? "" : "Name is not valid"), False);
I could opt to return true or false, depending if I want to allow invalid values (and perhaps misusing the validator classes). By removing the clearing of the error info, the error info preserves, even if I opt to return true.
As the consequences behind returning true or false is different if you'd remove the clearing of the error info, some customers whom rely on you clearing the error info might get unexpected behaviour (duh). Therefore, I opt for a setting, which has a clearing error info behaviour by default. Perhaps you could add this setting to the validator classes, and do a check on it in SetValue(). I don't see any reasons why you wouldn't do this, besides the reason that it isn't designed to be used this way. But you've once decided to implement IDataErrorInfo, and therefore allowing us to set it within Validator classes. Perhaps not the best design choice, but it is a good pragmatic choice IMO (it just damn easy the hook it into the UI). I just don't like you "dictating" the use of IDataErrorInfo.