protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (value == null)
return null;
IEntity entity = (IEntity)validationContext.ObjectInstance;
dynamic entityContext;
Type type = validationContext.ObjectType;
while (type.Assembly.IsDynamic)
type = type.BaseType;
entityContext = validationContext.GetService(typeof(IEntityContext<>).MakeGenericType(type));
if (entityContext == null)
return null;
if (value is string && IsCaseSensitive)
value = ((string)value).ToLower();
ParameterExpression parameter = Expression.Parameter(type);
Expression left = Expression.NotEqual(Expression.Property(parameter, "Index"), Expression.Constant(entity.Index));
Expression right;
if (value is string && IsCaseSensitive)
right = Expression.Equal(Expression.Call(Expression.Property(parameter, validationContext.MemberName), typeof(string).GetMethod("ToLower")), Expression.Constant(value));
else
right = Expression.Equal(Expression.Property(parameter, validationContext.MemberName), Expression.Constant(value));
Expression expression = Expression.And(left, right);
expression = Expression.Lambda(typeof(Func<,>).MakeGenericType(type, typeof(bool)), expression, parameter);
object where = _QWhereMethod.MakeGenericMethod(type).Invoke(null, new[] { entityContext.Query(), expression });
int count = (int)_QCountMethod.MakeGenericMethod(type).Invoke(null, new[] { where });
if (count != 0)
return new ValidationResult(string.Format("{0} can not be {1}, there is a same value in the database.", validationContext.MemberName, value));
else
return null;
}