2011-03-14 91 views
0

我在玩企業庫驗證塊。我有一個來自我在MVC項目中使用的Linq To Entities edmx文件的類。我想確保Nullable DateTime總是晚於DateTime。我在元數據類中使用屬​​性來創建默認規則集。當嘗試使用PropertyComparisonValidator進行驗證時,我得到以下異常:可用於空值的企業庫驗證塊<DateTime>

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

該問題與它是可空類型無關。我最終完全重新驗證了驗證。我使用屬性刪除了元數據類,並使用企業庫工具將驗證移至了配置。我終於能夠使用Property Comparison Validator來確保開始日期後的結束日期。最後的障礙是我無法使用默認的規則集。我在ASP.NET MVC網站中使用了這個功能,並且我能夠使用企業庫進行客戶端驗證。即使我不清楚爲什麼原始設置不起作用,我對整件事情感到非常滿意。