我正在启动一个新的 WPF MVVM 应用程序,并且试图了解如何“比以前更好地”处理验证 - 在我使用IDataErrorInfo
模型和 viewModel 之前。它工作正常,但以后很难扩展,并且在大多数情况下需要大量手动接线。我确信这IDataErrorInfo
仍然是连接 ViewModel 验证的好方法,但我想尝试使用 Enterprise Library 来验证模型,但是有没有办法一次实际验证一个属性而不是整个对象?
还是有其他巧妙的方法来使用 EL 生成的配置?
有什么想法或其他建议吗?
更新
我想我或多或少找到了我一直在寻找的东西:(来自http://entlib.codeplex.com/discussions/233057)
Type type = typeof(Customer);
PropertyInfo property = type.GetProperty("Name");
string ruleset = string.Empty;
var source = ValidationSpecificationSource.All;
var builder = new ReflectionMemberValueAccessBuilder();
Validator validator =
PropertyValidationFactory.GetPropertyValidator(
type, property, ruleset, source, builder);
ValidationResults results = validator.Validate(customer);
但这意味着我ViewModel
必须处理模型实例,而不是执行以下操作:
private string name;
public string Name
{
get { ... }
set { ...}
}
看起来我必须在模型实例上工作或在ViewModel
层中验证——我希望避免这种情况。
...还是我错过了对大多数人来说非常知名且显而易见的东西...?
欢迎任何建议。