0

的「Silverlight業務應用程序」模板與VS2010/Silverlight 4的捆綁使用的方法參數DataAnnotations在其領域服務類,它會被自動調用:與數據註釋驗證方法的參數屬性

 public CreateUserStatus CreateUser(RegistrationData user, 
     [Required(ErrorMessageResourceName = "ValidationErrorRequiredField", ErrorMessageResourceType = typeof(ValidationErrorResources))] 
     [RegularExpression("^.*[^a-zA-Z0-9].*$", ErrorMessageResourceName = "ValidationErrorBadPasswordStrength", ErrorMessageResourceType = typeof(ValidationErrorResources))] 
     [StringLength(50, MinimumLength = 7, ErrorMessageResourceName = "ValidationErrorBadPasswordLength", ErrorMessageResourceType = typeof(ValidationErrorResources))] 
     string password) 
    { /* do something */ } 

如果我需要在我的POCO類方法中實現這個功能,我如何獲得調用驗證的框架,或者我如何對所有參數進行必要的驗證(使用Validator或其他?)。

回答

1

我們走近它是這樣的:

我們有一個ValidationProperty類,它在將被用於驗證值(你可以使用任何你想要的)一個正則表達式。

ValidationProperty.cs

public class ValidationProperty 
{ 
    #region Constructors 

    /// <summary> 
    /// Constructor for property with validation 
    /// </summary> 
    /// <param name="regularExpression"></param> 
    /// <param name="errorMessage"></param> 
    public ValidationProperty(string regularExpression, string errorMessage) 
    { 
     RegularExpression = regularExpression; 
     ErrorMessage = errorMessage; 
     IsValid = true; 
    } 

    #endregion 

    #region Properties 

    /// <summary> 
    /// Will be true if this property is currently valid 
    /// </summary> 
    public bool IsValid { get; private set; } 

    /// <summary> 
    /// The value of the Property. 
    /// </summary> 
    public object Value 
    { 
     get { return val; } 
     set 
     { 
      if (this.Validate(value))//if valid, set it 
      { 
       val = value; 
      } 
      else//not valid, throw exception 
      { 
       throw new ValidationException(ErrorMessage); 
      } 
     } 
    } 
    private object val; 

    /// <summary> 
    /// Holds the regular expression that will accept a vaild value 
    /// </summary> 
    public string RegularExpression { get; private set; } 

    /// <summary> 
    /// The error message that will be thrown if invalid 
    /// </summary> 
    public string ErrorMessage { get; private set; } 

    #endregion 

    #region Private Methods 

    private bool Validate(object myValue) 
    { 
     if (myValue != null)//Value has been set, validate it 
     { 
      this.IsValid = Regex.Match(myValue.ToString(), this.RegularExpression).Success; 
     } 
     else//still valid if it has not been set. Invalidation of items not set needs to be handled in the layer above this one. 
     { 
      this.IsValid = true; 
     } 

     return this.IsValid; 
    } 

    #endregion 
} 

下面是我們如何將創建一個驗證屬性。注意公衆成員是一個字符串,但私下我使用'ValidationProperty'。

public string TaskNumber 
    { 
     get { return taskNumber.Value.ToString(); } 
     set 
     { 
      taskNumber.Value = value; 
      OnPropertyChanged("TaskNumber"); 
     } 
    } 
    private ValidationProperty taskNumber; 

現在,只要設置了值,業務層就會驗證它是一個有效值。如果不是,它只會拋出一個新的ValidationException(在ValidationProperty類中)。在您的xaml中,您需要將NotifyOnValidationError & ValidatesOnExceptions設置爲true。

<TextBox Text="{Binding TaskNumber, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}"/> 

通過這種方法,你可能有一個表單用於創建新的「用戶」和各領域將各自他們設置時間(我希望是有道理的)valitate。

這是我們用來驗證業務層的方法。我不確定這是否正是您想要的,但我希望它有幫助。

+0

當我回顧你的問題時,我意識到我以前讀過它錯了:(我以爲你正在創建一個屬性並試圖驗證你的對象的屬性,而不是一個方法,我想我從來沒有做過正是你想要做的,但也許我的答案會給你帶來一些想法。對不起! – JSprang 2010-06-03 13:16:12

+0

不用擔心 - 謝謝你的回覆。儘管你試圖做的大部分事情都可以用DataAnnotations來解決。 INotifyDataErrorInfo和INotifyPropertyChanged(或者僅僅是System.ServiceModel.DomainServices.Client.Entity的子類),並且您可以通過ValidationSummary控件獲取「free」的驗證錯誤UX。您可以通過擴展System.ComponentModel.DataAnnotations.ValidationAttribute來編寫自定義驗證裝飾器,或者甚至使用System.ComponentModel.DataAnnotations.CustomVal指向代碼中的現有驗證方法idation屬性。 – lo5 2010-06-04 02:39:20

+1

是的,這就是我們正在做的。我們使用這種方法,而不是DataAnnotations,以便我們可以將驗證邏輯放在業務層上,如果這樣做合理的話。 – JSprang 2010-06-04 12:40:29