Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

Commit

Permalink
Refactored GetValidators method
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodejunkie committed Mar 2, 2014
1 parent 9f207dd commit 0080553
Showing 1 changed file with 21 additions and 13 deletions.
Expand Up @@ -32,22 +32,19 @@ public IEnumerable<IPropertyValidator> GetValidators(Type type)
var typeDescriptor =
new AssociatedMetadataTypeTypeDescriptionProvider(type).GetTypeDescriptor(type);

var propertyDescriptors =
typeDescriptor.GetProperties();

var results =
new List<IPropertyValidator>();

var classAttributes =
typeDescriptor.GetAttributes().OfType<ValidationAttribute>();
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<PropertyValidator> GetPropertyValidators(ICustomTypeDescriptor typeDescriptor)
{
var propertyDescriptors =
typeDescriptor.GetProperties();

foreach (PropertyDescriptor descriptor in propertyDescriptors)
{
Expand All @@ -61,10 +58,21 @@ public IEnumerable<IPropertyValidator> GetValidators(Type type)
Descriptor = descriptor
};

results.Add(validator);
yield return validator;
}
}

return results;
private PropertyValidator GetTypeValidator(ICustomTypeDescriptor typeDescriptor)
{
var classAttributes =
typeDescriptor.GetAttributes().OfType<ValidationAttribute>();

var classValidator =
new PropertyValidator
{
AttributeAdaptors = this.GetAttributeAdaptors(classAttributes)
};
return classValidator;
}

private IDictionary<ValidationAttribute, IEnumerable<IDataAnnotationsValidatorAdapter>> GetAttributeAdaptors(IEnumerable<ValidationAttribute> attributes)
Expand Down

0 comments on commit 0080553

Please sign in to comment.