ValidationContextException

Dec 10, 2009 at 12:32 AM

I'm creating a library and i decided to use ValidationAspect.

What bugs me is that for example if i have a string property having 2 validation :

 

[NotNull]
        [MaximumLength(100)]
        public string Name
        {
            get { return this.TaskEntity.name; }
            set { this.TaskEntity.name = value; }
        }
 
If any of those validation fails, a ValidationContextException is thrown,encapsulating one or both ValidateNotNullException or ValidateMaximumLenghtException and the
ValidationContextException message is set to the first of those exception.
 
The problem i have is that as its a library that going to be used , even if the message is valid , the exception thrown is meaningless to my users in term of type returned.

I understand its benefits when calling validate on the object, but why not just throw the first exception to occurs in case of property interceptor instead of throwing this container exception ?

Like in my case , i know that there will never be more than one exception at a time because both validation are mutually exclusive (looked at the code for notnull and maximumlenght but i don't know if its the case for all validator).
If there is only one exception happening throw the exception instead of the context one ?

It makes management of exception a bit awkward as if you have only one validator, it throws the right exception associated and if it has more than one validator, a ValidationContextExcewption is thrown. Because if i do my own validation and throw exception , i will throw either the null validation or maximumlenght but not both because it can't happen.

It seem its quite a while since your last update, i would probably have to make modification myself ,

but what is your opinion ? Do i have a valid point ? What could be the work around , could we change the behavior when intercepting properties setter and only throw the first exception instead of ValidationContextException  ?

Great work between :)

Coordinator
Dec 10, 2009 at 10:23 AM

A ValidationContextException is thrown as it's override implementation of ValidationException.Messages returns an IEnumerable of the failed validation messages. As developers are able to create their own validators it is possible that multiple validators may fail and it is important (I think) to report to the user all the reasons the value they set is invalid.

Is the actual Type of the exception important to you? I typically catch ValidationException and report the Messages. If you only want to report one failed validation message, you can call the Message property.