|
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
namespace Application.Common.Validator
{
/// <summary>
/// Validator based on Data Annotations.
/// This validator use IValidatableObject interface and
/// ValidationAttribute ( hierachy of this) for
/// perform validation
/// </summary>
public class DataAnnotationsEntityValidator
: IEntityValidator
{
#region Private Methods
/// <summary>
/// Get erros if object implement IValidatableObject
/// </summary>
/// <typeparam name="TEntity">The typeof entity</typeparam>
/// <param name="item">The item to validate</param>
/// <param name="errors">A collection of current errors</param>
void SetValidatableObjectErrors<TEntity>(TEntity item, List<string> errors) where TEntity : class
{
if (typeof(IValidatableObject).IsAssignableFrom(typeof(TEntity)))
{
var validationContext = new ValidationContext(item, null, null);
var validationResults = ((IValidatableObject)item).Validate(validationContext);
errors.AddRange(validationResults.Select(vr => vr.ErrorMessage));
}
}
/// <summary>
/// Get errors on ValidationAttribute
/// </summary>
/// <typeparam name="TEntity">The type of entity</typeparam>
/// <param name="item">The entity to validate</param>
/// <param name="errors">A collection of current errors</param>
void SetValidationAttributeErrors<TEntity>(TEntity item, List<string> errors) where TEntity : class
{
var result = from property in TypeDescriptor.GetProperties(item).Cast<PropertyDescriptor>()
from attribute in property.Attributes.OfType<ValidationAttribute>()
where !attribute.IsValid(property.GetValue(item))
select attribute.FormatErrorMessage(string.Empty);
if (result != null
&&
result.Any())
{
errors.AddRange(result);
}
}
#endregion
#region IEntityValidator Members
/// <summary>
///
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="item"></param>
/// <returns></returns>
public bool IsValid<TEntity>(TEntity item) where TEntity : class
{
if (item == null)
return false;
List<string> validationErrors = new List<string>();
SetValidatableObjectErrors(item, validationErrors);
SetValidationAttributeErrors(item, validationErrors);
return !validationErrors.Any();
}
/// <summary>
///
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="item"></param>
/// <returns></returns>
public IEnumerable<string> GetInvalidMessages<TEntity>(TEntity item) where TEntity : class
{
if (item == null)
return null;
List<string> validationErrors = new List<string>();
SetValidatableObjectErrors(item, validationErrors);
SetValidationAttributeErrors(item, validationErrors);
return validationErrors;
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.