Unhandled exception for properties without any validation rule

Topics: Developer Forum, User Forum
Sep 8, 2007 at 5:44 AM
Edited Sep 9, 2007 at 1:21 AM
from an issue raise by mikeihttp://www.codeplex.com/ValidationFramework/WorkItem/View.aspx?WorkItemId=12636
============================================
During play with ValidationFramework I found it frustrating when I want to list the rules for Property, that currently has no validation rules.

Assume:
Class QueryCheckService has Agent property but in XML configuration file there is no rule for this property. Then following code will cause the exception
The given key was not present in the dictionary.
Source File: C:\Utils\ValidationFramework\Utilities\AutoKeyDictionary.cs Line: 309
Type type = typeof(QueryCheckService);
TypeDescriptor typeDescriptor = TypeCache.GetType(type.TypeHandle);
Debug.WriteLine("[VWS] Rules counter for type [" + type.FullName + "] " + 
typeDescriptor.Properties["Agent"].Rules.Count);
Debug.WriteLine("[VWS] AppPath: " + HttpRuntime.AppDomainAppPath);
It can be done this way by intention, but it isn't intuitive to find the reason of the Exception, especially in big XML configuration files.
Sep 8, 2007 at 6:02 AM
Edited Sep 9, 2007 at 1:20 AM
Mike
I am sorry you are being frustrated but the XML configuration.
To address your immediate problem of the exception being thrown i recommend you do a ContainsKey before attempting to access a PropertyDescriptor. This way of it does not exists no exception will be thrown
Something like this:
Type type = typeof(QueryCheckService);
TypeDescriptor typeDescriptor = TypeCache.GetType(type.TypeHandle);
if (typeDescriptor.Properties.ContainsKey("Agent"))
{
    Debug.WriteLine(string.Format("[VWS] Rules counter for type [{0}] on property 'Agent' = {1}", type.FullName, 
typeDescriptor.Properties["Agent"].Rules.Count));
}
else
{
    Debug.WriteLine(string.Format("[VWS] No Rules found for type [{0}]  on property 'Agent'", type.FullName));
}

As for trying to work out if your configuration is loading as expected try the code below. It will walk through your assembly and write out the rules for each property on each type. Ensure that you call this after you have added you xml config through the ConfigurationService.

        public void WriteRules()
        {
            Assembly assembly = typeof (QueryCheckService).Assembly;
            IList<TypeDescriptor> typeDescriptors = RuleDescriber.GetPropertyRules(assembly);
            StringBuilder typeStringBuilder = new StringBuilder("\r\n\r\nTypes :\r\n=================================\r\n");
 
            if (typeDescriptors.Count == 0)
            {
                typeStringBuilder.Append("No property validation found.");
            }
            else
            {
                foreach (TypeDescriptor typeDescriptor in typeDescriptors)
                {
                    Type type = Type.GetTypeFromHandle(typeDescriptor.RuntimeTypeHandle);
                    typeStringBuilder.AppendLine(string.Format("Class : {0}", type.FullName));
                    typeStringBuilder.AppendLine(string.Format("Base Class : {0}", type.BaseType.FullName));
                    foreach (PropertyDescriptor propertyDescriptor in typeDescriptor.Properties)
                    {
                        typeStringBuilder.AppendLine(string.Format("\tProperty : {0}", propertyDescriptor.Name));
                        foreach (Rule rule in propertyDescriptor.Rules)
                        {
                            typeStringBuilder.AppendLine(string.Format("\t\tRule : {0}", rule.RuleInterpretation));
                        }
                    }
                    typeStringBuilder.AppendLine("");
                    typeStringBuilder.AppendLine("");
                }
            }
            Debug.WriteLine(typeStringBuilder.ToString());
        }

Also, is there anything you can think of that would make it easier for people starting to use the XML configuration?

Regards
Simon