我使用IValidateableObject。如果你的对象类实现了这个接口,它暴露你可以覆盖的validate方法,那么你可以只在需要时调用这个验证对象object.validate。它返回true或false。在该验证函数中,您可以循环访问对象属性,并根据您的自定义bunisness逻辑将所需的列添加到IDataerrorInfo中。我通过创建一个由我的模型/对象类继承的Validationbase类来实现此目的。
这里是我的验证基类的例子,你可以继承到模型/对象类:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.ComponentModel;
using System.Collections.Concurrent;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
public class ValidationBase : IValidatableObject, IDataErrorInfo, INotifyPropertyChanged
{
#region "DECLARATIONS"
protected Dictionary<string, string> _propertyErrors = new Dictionary<string, string>();
protected List<ValidationResult> _validationResults = new List<ValidationResult>();
public bool HasErrors {
get { return (_propertyErrors.Count + _validationResults.Count) > 0; }
}
#endregion
#region "IValidatableObject IMPLEMENTATION"
public virtual IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
return null;
}
#endregion
#region "iDataError OBJECTS"
//Returns an error message
//In this case it is a general message, which is
//returned if the list contains elements of errors
public string Error {
get {
if (_propertyErrors.Count > 0) {
return "Object data is invalid";
} else {
return null;
}
}
}
public string this[string columnName] {
get {
if (_propertyErrors.ContainsKey(columnName)) {
return _propertyErrors[columnName].ToString();
} else {
return null;
}
}
}
#endregion
#region "IDataError FUNCTIONS"
//Adds an error to the collection, if not already present
//with the same key
protected void AddError(string columnName, string msg)
{
if (!_propertyErrors.ContainsKey(columnName)) {
_propertyErrors.Add(columnName, msg);
OnPropertyChanged(columnName);
}
}
//Removes an error from the collection, if present
protected void RemoveError(string columnName)
{
if (_propertyErrors.ContainsKey(columnName)) {
_propertyErrors.Remove(columnName);
OnPropertyChanged(columnName);
}
}
public void ClearErrors()
{
_propertyErrors.Clear();
}
#endregion
#region "INotifyPropertyChanged IMPLEMENTATION"
public event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged;
public delegate void PropertyChangedEventHandler(object sender, PropertyChangedEventArgs e);
protected virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
如果您正在使用实体框架模型,我相信validate方法将以前被称为目的是通过db.SaveChanges保存
这里是你如何实现你的模型中validationbase:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public partial class vendor : ValidationBase
{
#region "PROPERTIES"
public bool HasChanges { get; set; }
#endregion
#region "VALIDATION FUNCTIONS"
public override IEnumerable<ComponentModel.DataAnnotations.ValidationResult> Validate(ComponentModel.DataAnnotations.ValidationContext validationContext)
{
return base.Validate(validationContext);
PropertyValitaion(true);
}
public void PropertyValitaion(bool bAllProperties, string sProperty = "")
{
//your property specific logic goes here
}
#endregion
}
你PropertyValitaion(真)之前已经取得回报; – GasDev 2015-11-05 10:30:37