Suggestion

Topics: Developer Forum
Jan 29, 2007 at 3:17 PM
I think it would be really nice if the ValidationResult would hold the type information as well (the type that declares the property, not the property's type). Here is the scenario (which I think is a very likely scenario): you have a big composed object and you want to validate it. You will write a recursive method that will navigate the object graph using reflection and will validate every level (type). At the end you get left with a bunch of validation results, some of them having descriptions such as "The member 'Id' is required". It would be nice if we could relate the validation result to the declaring type at this point. Hope this makes sense.

Cheers,
Daniel
Coordinator
Jan 30, 2007 at 10:34 AM
That makes sense
I will have a look into doing this, although it will not happen for a few weeks. (I am working on a few other things at the moment)
In the interim, why not store the type against each list of ValidationResult(s)
Here is a simple implementation of how I think it could work.

IList<ValidatableBase> myValidatableClasses = null;//your list of classes to validate
Dictionary<Type, IList<ValidationResult>> myDictionary = new Dictionary<Type, IList<ValidationResult>>();
foreach (ValidatableBase validatableBase in myValidatableClasses)
{
myDictionary.Add(validatableBase.GetType(), validatableBase.ValidatorResultsInError);
}

Now you have each list of results with their associated types. You could do the same with a recursive call through passing the dictionary in as a parameter of the method.
Coordinator
Jan 30, 2007 at 10:39 AM
This discussion has been copied to Work Item 7789. You may wish to continue further discussion there.
Jan 30, 2007 at 10:40 PM
Thanks mate. That is pretty much what I've done.
Again, congrats for the framework. It is really cool.
Coordinator
Jan 30, 2007 at 11:21 PM
Daniel

The only problem with the approach I suggested is the performance and memory hit of using Type. You should actually be using RuntimeTypeHandle to uniquely identify types.
Have a read of these pages.
http://msdn.microsoft.com/msdnmag/issues/05/07/Reflection/
http://blogs.msdn.com/vancem/archive/2006/10/01/779503.aspx
Coordinator
May 4, 2007 at 12:40 PM
Edited May 4, 2007 at 12:40 PM
Daniel
I finally got around to implementing this.
If you get the latest check in you can use the following code to get type and method information from a validation result
public static Type GetTypeFromPropertyValidationResult(ValidationResult result)
{
	InfoDescriptor infoDescriptor = result.Rule.InfoDescriptor;
	PropertyDescriptor propertyDescriptor = (PropertyDescriptor) infoDescriptor;
	TypeDescriptor typeDescriptor = propertyDescriptor.TypeDescriptor;
	return Type.GetTypeFromHandle(typeDescriptor.RuntimeTypeHandle);
}
 
 
public static MethodBase GetMethodInfoFromParameterValidationResult(ValidationResult result)
{
	InfoDescriptor infoDescriptor = result.Rule.InfoDescriptor;
	ParameterDescriptor parameterDescriptor = (ParameterDescriptor)infoDescriptor;
	MethodDescriptor methodDescriptor = parameterDescriptor.Method;
	return MethodInfo.GetMethodFromHandle(methodDescriptor.RuntimeMethodHandle);
}
This functionality will be included in the next release.
May 21, 2007 at 12:54 PM
Thanks mate, that is fantastic.
Coordinator
May 27, 2007 at 11:35 AM
No worries.
Keep the feature requests coming