开发者

WPF + MVVM + Enterprise Library Validation Block

I'm starting a new WPF MVVM App and I'm trying to get an idea how to handle validation "better than I have done before" - before I used IDataErrorInfo on the model and viewModel. It worked ok but it was difficult to extend later on and needed lots of manual wiring in most cases. I'm sure that IDataErrorInfo is still a good way for hooking up the ViewModel validation but I would like to have a go with the Enterprise Library to validate the Model but is there a way to ac开发者_如何学Ctually validate one property at a time instead of a whole object?

Or is there some other clever way to use the config generated by EL?

Any ideas or other suggestions?

UPDATE

I think I found something more or less what I've been looking for: (from 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);

But that would mean that my ViewModel has to work on a model instance rather than doing something like:

private string name;
public string Name
{
   get { ... }
   set { ...}
}

It looks like I have to work on a model instance or validate in the ViewModel layer - which I was hoping to avoid.

... or am I missing something really well-known and obvious to most people ... ?

Any suggestions welcome.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜