我正在玩企业库验证块。我有一个来自我在MVC项目中使用的Linq To Entities edmx文件的类。我想确保Nullable DateTime总是晚于DateTime。我正在使用元数据类中的属性来创建默认规则集。尝试使用PropertyComparisonValidator进行验证时,我得到了异常:
PropertyComparisonValidatorAttribute类型的验证属性不能用于验证值。
我认为我无法将Nullable类型与结构进行比较,因此我专门编写了下面的自定义类来解决这个感知问题。我仍然得到这个例外:
NullableDateComparisonValidatorAttribute类型的验证属性不能用于验证值。
接下来,我尝试了企业库中的自我验证,并且在验证时无法触发。我暂时停顿了,直到我弄明白这一点。请建议一个不太难看的解决方案\解决方法。
using System;
using System.Reflection;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
namespace IdahoUtility
{
public class NullableComparisonValidator<T> : Validator<Nullable<T>>
where T: struct, IComparable
{
protected string propertyToCompare { get; set; }
protected ComparisonOperator comparisionOperator { get; set; }
protected T TargetProperty(object currentTarget)
{
if (null == currentTarget)
{
throw new ArgumentNullException("currentTarget");
}
Type t = currentTarget.GetType();
PropertyInfo pInfo = t.GetProperty(propertyToCompare);
object oValue = pInfo.GetValue(currentTarget, null);
if (oValue.GetType() != typeof(T))
{
throw new InvalidOperationException(string.Format("Property compared must be a {0}!",typeof(T)));
}
return (T)oValue;
}
public NullableComparisonValidator(string PropertyToCompare, ComparisonOperator cmpOp)
:base(null,null)
{
if (string.IsNullOrWhiteSpace(PropertyToCompare))
{
throw new ArgumentException("PropertyToCompare is Invalid!", "PropertyToCompare");
}
propertyToCompare = PropertyToCompare;
comparisionOperator = cmpOp;
}
protected override void DoValidate(Nullable<T> objectToValidate, object currentTarget, string key, ValidationResults validationResults)
{
// ignore nulls
if (null == objectToValidate)
{
return;
}
switch (comparisionOperator)
{
case ComparisonOperator.Equal:
DoValidateEqual(objectToValidate, currentTarget, key, validationResults);
break;
case ComparisonOperator.GreaterThan:
DoValidateGreaterThan(objectToValidate, currentTarget, key, validationResults);
break;
case ComparisonOperator.GreaterThanEqual:
DoValidateGreaterThanEqual(objectToValidate, currentTarget, key, validationResults);
break;
case ComparisonOperator.LessThan:
DoValidateLessThan(objectToValidate, currentTarget, key, validationResults);
break;
case ComparisonOperator.LessThanEqual:
DoValidateLessThanEqual(objectToValidate, currentTarget, key, validationResults);
break;
case ComparisonOperator.NotEqual:
DoValidateNotEqual(objectToValidate, currentTarget, key, validationResults);
break;
}
}
private void DoValidateLessThanEqual(Nullable<T> objectToValidate, object currentTarget, string key, ValidationResults validationResults)
{
if (objectToValidate.Value.CompareTo(TargetProperty(currentTarget)) > 0)
{
LogValidationResult(validationResults, string.Format("Should be less than or equal to {0}!", propertyToCompare), currentTarget, key);
}
}
private void DoValidateLessThan(Nullable<T> objectToValidate, object currentTarget, string key, ValidationResults validationResults)
{
if (objectToValidate.Value.CompareTo(TargetProperty(currentTarget)) >= 0)
{
LogValidationResult(validationResults, string.Format("Should be less than {0}!", propertyToCompare), currentTarget, key);
}
}
private void DoValidateGreaterThanEqual(Nullable<T> objectToValidate, object currentTarget, string key, ValidationResults validationResults)
{
if (objectToValidate.Value.CompareTo(TargetProperty(currentTarget)) < 0)
{
LogValidationResult(validationResults, string.Format("Should be greater than or equal {0}!", propertyToCompare), currentTarget, key);
}
}
private void DoValidateGreaterThan(Nullable<T> objectToValidate, object currentTarget, string key, ValidationResults validationResults)
{
if (objectToValidate.Value.CompareTo(TargetProperty(currentTarget)) <= 0)
{
LogValidationResult(validationResults, string.Format("Should be greater than {0}!", propertyToCompare), currentTarget, key);
}
}
private void DoValidateEqual(Nullable<T> objectToValidate, object currentTarget, string key, ValidationResults validationResults)
{
if (objectToValidate.Value.CompareTo(TargetProperty(currentTarget)) != 0)
{
LogValidationResult(validationResults, string.Format("Should be equal to {0}!", propertyToCompare), currentTarget, key);
}
}
private void DoValidateNotEqual(Nullable<T> objectToValidate, object currentTarget, string key, ValidationResults validationResults)
{
if (objectToValidate.Value.CompareTo(TargetProperty(currentTarget)) == 0)
{
LogValidationResult(validationResults, string.Format("Should not be equal to {0}!", propertyToCompare), currentTarget, key);
}
}
protected override string DefaultMessageTemplate
{
get { return "{0}"; }
}
}
}
答案 0 :(得分:0)
这个问题与它是一个可以为空的类型无关。我最终完全重新进行了验证。我删除了带有属性的元数据类,并使用企业库工具将我的验证移至配置。我终于能够使用属性比较验证器来确保结束日期在开始日期之后。最后的障碍是我无法使用默认的规则集。我在ASP.NET MVC网站中使用它,我能够使用企业库进行客户端验证工作。我实际上对整件事感到非常满意,即使我不清楚为什么原来的设置不起作用。