2012-09-07 2 views
0

부모 클래스가 INotifyPropertyChanged를 구현하고 있고 부모 클래스가 여러 자식을 가지고 있습니다. 자식에는 PropertyChanged를 모두 호출하는 다양한 속성이 있습니다. 유효성 검사를 추가하고 싶지만 모든 하위 클래스에 대한 유효성 검사를 작성하고 싶지는 않습니다. 유효성 검사 규칙은 데이터베이스에서 제공되므로 결국 각 자식에 대한 유효성 검사 규칙을 가져와 규칙과 비교하여 값을 확인해야합니다. 그랬다면 너무 많은 중복 코드가있을 것이라고 생각합니다. PropertyChanged가 값 자체의 문자열 값을 트리거하기 때문에 상위 수준에 배치하려고합니다.다른 속성을 구현하는 하위 클래스에 대한 유효성 검사를 제공하는 방법?

모든 하위 클래스에 대한 유효성 검사 메서드를 작성할 필요가 없도록 부모 클래스에 유효성 검사 메서드를 포함 할 수 있습니까? 모든 자녀 클래스의 속성은 다릅니다.

다음은 현재 내가 갖고있는 것으로, 하위 클래스에서 유효성 검사가 있습니다.

public Parent : INotifyChanged { 
    /// <summary> 
    /// Occurs when a property is changed 
    /// </summary> 
    public event PropertyChangedEventHandler PropertyChanged; 

    /// <summary> 
    /// Raises the <see cref="PropertyChanged"/> for a given 
    /// property. 
    /// </summary> 
    /// <param name="propertyName"></param> 
    protected void OnPropertyChanged(String propertyName) { 
     // Get the hanlder 
     PropertyChangedEventHandler handler = this.PropertyChanged; 

     // Check that the event handler is not null 
     if(null != handler) { 
      // Fire the event 
      handler(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

자식 1 등급 :

public Child1 : Parent, IDataErrorInfo { 
private Dictionary<string, string> m_validationErrors = new Dictionary<string, string>(); 

    private void Validate() { 
     this.RemoveError("Child1Description"); 
     if(!Regex.IsMatch(Child1Description, "^([a-zA-Z '-]+)$") && !String.IsNullOrWhiteSpace(Description)) { 
      this.AddError("Child1Description", "Only non-numerics allowed."); 
     } 
    } 

    private void AddError(string columnName, string msg) { 
     if(!m_validationErrors.ContainsKey(columnName)) { 
      m_validationErrors.Add(columnName, msg); 
     } 
    } 

    private void RemoveError(string columnName) { 
     if(m_validationErrors.ContainsKey(columnName)) { 
      m_validationErrors.Remove(columnName); 
     } 
    } 

    public string Error { 
     get { 
      if(m_validationErrors.Count > 0) { 
       return "Field data is invalid."; 
      } 
      else return null; 
     } 
    } 

    public string this[string columnName] { 
     get { 
      if(m_validationErrors.ContainsKey(columnName)) { 
       return m_validationErrors[columnName]; 
      } 
      else { 
       return null; 
      } 
     } 
    } 
    /// <summary> 
    /// Description of the air entity 
    /// </summary> 
    public string Child1Description { 
     get { 
      return Child1description; 
     } 
     set { 
      description = value; 
      Validate(); 
      OnPropertyChanged("Child1Description"); 
     } 
    } 
} 

자식 2 클래스 :

public Child2 : Parent, IDataErrorInfo { 
private Dictionary<string, string> m_validationErrors = new Dictionary<string, string>(); 

    private void Validate() { 
     this.RemoveError("Child2Description"); 
     if(!Regex.IsMatch(Child2Description, "^([a-zA-Z '-]+)$") && !String.IsNullOrWhiteSpace(Description)) { 
      this.AddError("Child2Description", "Only non-numerics allowed."); 
     } 
    } 

    private void AddError(string columnName, string msg) { 
     if(!m_validationErrors.ContainsKey(columnName)) { 
      m_validationErrors.Add(columnName, msg); 
     } 
    } 

    private void RemoveError(string columnName) { 
     if(m_validationErrors.ContainsKey(columnName)) { 
      m_validationErrors.Remove(columnName); 
     } 
    } 

    public string Error { 
     get { 
      if(m_validationErrors.Count > 0) { 
       return "Field data is invalid."; 
      } 
      else return null; 
     } 
    } 

    public string this[string columnName] { 
     get { 
      if(m_validationErrors.ContainsKey(columnName)) { 
       return m_validationErrors[columnName]; 
      } 
      else { 
       return null; 
      } 
     } 
    } 
    /// <summary> 
    /// Description of the air entity 
    /// </summary> 
    public string Child2Description { 
     get { 
      return Child2description; 
     } 
     set { 
      description = value; 
      Validate(); 
      OnPropertyChanged("Child2Description"); 
     } 
    } 
} 
+0

Q : 상위 클래스에서 유효성 검사 방법을 사용할 수 있습니까? A : 네. 왜 안돼? 당신의 질문은 정확히 무엇입니까? 코드를 게시 할 수 있습니까? – paulsm4

+0

@ paulsm4 죄송합니다. 정리를 시도했습니다. 코드가 추가되었습니다. –

답변

0

필자는 속성 이름, 속성 값 및 유효성 검사에 사용하려는 규칙 목록을 전달했습니다. Validate 메서드를 부모에 저장하면 어떤 자식이 사용했는지에 관계없이 실행되므로 자체 규칙을 사용하지만 동일한 유효성 검사 오류 메시지 사전을 유지합니다.

protected void Validate(string propertyName, string propertyValue, List<ValidRule> validRules) { 
     string temp = propertyValue.ToString(); 
     this.RemoveError(propertyName); 
     if(propertyName.Equals("Description")) { 
      foreach(ValidRule validRule in validRules) { 
       if(!Regex.IsMatch(propertyValue, validRule.Rule) && !String.IsNullOrWhiteSpace(propertyValue)) { 
        this.AddError(propertyName, validRule.ErrorMessage); 
        break; 
       } 
      } 
     } 
} 
0

나는 당신이 당신이 원하는 것을 할 수있을거야 믿지 않는다.

사소한 경우에는 효과를 낼 수 있습니다. 좀 더 복잡한 유형으로 이동하면 자녀가 아닌 부모에게 유효성 검사를함으로써 많은 노력을 절약한다는 것을 알 수 없습니다.

평범한 경우는 여러 어린이가 유사한 유형의 속성을 사용하는 경우입니다. 속성을 같은 방식으로 호출하도록 적용한 다음 속성 이름을 트리거하는 부모 내에 유효성 검사 규칙을 작성할 수 있습니다. 그러나 당신은 그 속성들이 부모의 일부분이어야하고 그 자식에 의해 상속되어야한다고 주장 할 수 있습니다.

더 복잡한 경우는 다른 어린이의 속성과 비슷하거나 거의 비슷한 각 어린이의 개별 속성입니다. 자녀 또는 부모에게 유효성 확인 코드를 넣을지 여부는 아무런 차이가 없습니다. 유효성을 검사하려는 개별 속성마다 유효성 검사 코드를 작성해야합니다.

유효성 검사 규칙이 DB에 저장되면 하위에 해당 속성의 유효성 검사 규칙을 검색 할 수있는 메서드를 부모에게 작성할 수 있습니다. 자식은 여전히 ​​자신의 속성을 검증하지만 규칙에 액세스하기위한 공통 코드가 있어야합니다.

0

사실 그것은 당신이 원하는 것이라고 생각하는 방식이 아닙니다. 다음은 비슷한 작업을 수행하기위한 단계입니다.

  1. Microsoft.Practices.EnterpriseLibrary.Validation 참조를 사용하기 때문에 우선 Microsoft 엔터프라이즈 라이브러리를 구하십시오.
  2. Validator<T> (Enterprise 라이브러리의 일부 임)을 상속하는 유효성 검사 클래스를 만듭니다.
  3. DoValidate(T objectToValidate, object currentTarget, string key, ValidationResults validationResults) 메서드를 재정의하고 currentTarget이 유효성을 검사중인 개체입니다. 현재 대상에서 유효성 검사 규칙을 가져올 수 있습니다.
  4. 그런 다음 유효성 검사에 대한 특성을 작성하여 ValueValidatorAttribute에서 상속하도록 만듭니다.
  5. 속성 클래스에서 DoCreateValidator(Type targetType, Type ownerType, MemberValueAccessBuilder memberValueAccessBuilder, ValidatorFactory validatorFactory) 메서드를 대체합니다. 이 최초의 5 단계 일단

는 당신이 확인하고 검증 클래스에서 사용하는 유효성 검사 규칙을 선택할 수 있도록하려면 속성을 속성 수 있음을 의미 완료 (규칙이나 사전 또는 규칙의 목록은 속성에 수행 완전히 당신의 선택).

다음 단계는 인터페이스 IDataErrorinfo을 상위 클래스로 이동하고 Microsoft.Practices.EnterpriseLibrary.Validation.Validation.Validate(this); 호출 결과를 가져 오는 Validate() 메서드를 생성합니다.이 메서드는 유효성 검사 오류가 발생한 경우이를 반환합니다.

메서드 호출을 배치하려는 위치에 따라 다릅니다. 테스트 할 때 가장 간단한 방법은 OnPropertyChanged 메서드에 배치하는 것입니다.

관련 문제