부모 클래스가 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");
}
}
}
Q : 상위 클래스에서 유효성 검사 방법을 사용할 수 있습니까? A : 네. 왜 안돼? 당신의 질문은 정확히 무엇입니까? 코드를 게시 할 수 있습니까? – paulsm4
@ paulsm4 죄송합니다. 정리를 시도했습니다. 코드가 추가되었습니다. –