From 00805539a033fd1566415b88751591309a871193 Mon Sep 17 00:00:00 2001 From: Andreas Hakansson Date: Sun, 2 Mar 2014 22:45:02 +0100 Subject: [PATCH] Refactored GetValidators method --- .../DefaultPropertyValidatorFactory.cs | 34 ++++++++++++------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/src/Nancy.Validation.DataAnnotations/DefaultPropertyValidatorFactory.cs b/src/Nancy.Validation.DataAnnotations/DefaultPropertyValidatorFactory.cs index a218cd083b..4f551981a3 100644 --- a/src/Nancy.Validation.DataAnnotations/DefaultPropertyValidatorFactory.cs +++ b/src/Nancy.Validation.DataAnnotations/DefaultPropertyValidatorFactory.cs @@ -32,22 +32,19 @@ public IEnumerable GetValidators(Type type) var typeDescriptor = new AssociatedMetadataTypeTypeDescriptionProvider(type).GetTypeDescriptor(type); - var propertyDescriptors = - typeDescriptor.GetProperties(); - var results = new List(); - var classAttributes = - typeDescriptor.GetAttributes().OfType(); + results.Add(this.GetTypeValidator(typeDescriptor)); + results.AddRange(this.GetPropertyValidators(typeDescriptor)); - var classValidator = - new PropertyValidator - { - AttributeAdaptors = this.GetAttributeAdaptors(classAttributes) - }; + return results; + } - results.Add(classValidator); + private IEnumerable GetPropertyValidators(ICustomTypeDescriptor typeDescriptor) + { + var propertyDescriptors = + typeDescriptor.GetProperties(); foreach (PropertyDescriptor descriptor in propertyDescriptors) { @@ -61,10 +58,21 @@ public IEnumerable GetValidators(Type type) Descriptor = descriptor }; - results.Add(validator); + yield return validator; } + } - return results; + private PropertyValidator GetTypeValidator(ICustomTypeDescriptor typeDescriptor) + { + var classAttributes = + typeDescriptor.GetAttributes().OfType(); + + var classValidator = + new PropertyValidator + { + AttributeAdaptors = this.GetAttributeAdaptors(classAttributes) + }; + return classValidator; } private IDictionary> GetAttributeAdaptors(IEnumerable attributes)