external validators & cinch

Feb 21, 2011 at 11:06 PM
Edited Feb 22, 2011 at 4:00 AM

I have a project that uses NHibernate Validator, and was wondering if anyone else is using Cinch with an external validator; By external, I mean external to the UI layer itself, validating the model, but with the results translated into cinch view models for presentation to the user.

I'm thinking maybe some sort of ValidatorRule:

puclic class ValidatorRule : Rule{
Rules[] _rules;
object _model;
IValidator _validator;

ValidatorRule(object model, IValidator validator){
    _model = model;
    _validator = validator;
}

public override bool ValidateRule(object rule)
{
    var validatorRule = rule as ValidatorRule;
    var results = validatorRule.Validate(_model);
    _translateToCinchRules(results);

    return _validatorRule._validator.IsValid(_model);
}

}

I don't know Cinch well enough to say much more. Has anyone done anything like this yet? Any suggestions?

Cheers, 

Berryl

Feb 22, 2011 at 3:09 PM
Edited Feb 22, 2011 at 3:14 PM

It should not be that bad, you can simply make sure you override the correct properties/methods

 

For example

 

Cinch.ValidatingObject and Cinch.ValidatingViewModelBase, you can simple override these methods/properties

  • public virtual bool IsValid
  • public virtual string Error
  • public virtual string this[string propertyName]
  • public virtual ReadOnlyCollection<Rule> GetBrokenRules()
  • public virtual ReadOnlyCollection<Rule> GetBrokenRules(string property)

 

Ok you will have some work to create a ReadOnlyCollection<Rule> to keep Cinch happy, but really in terms of how WPF validation works, you really only need to override the methods that are there for the IDataErrorInfo interface.

So that would really on mean these ones

  • public virtual bool IsValid
  • public virtual string Error
  • public virtual string this[string propertyName]

You should be fine to return null for these other methods

  • public virtual ReadOnlyCollection<Rule> GetBrokenRules()
  • public virtual ReadOnlyCollection<Rule> GetBrokenRules(string property)

As they will no longer be used if you override the methods above to satisfy IDataErrorInfo where you hook into your NHibernate validation mechanism.

I think you need to understand the IDataErrorInfo interface, so if you do not, and do not know how this works with WPF/SL, that should be your 1st stop. But all these methods are virtual, so just override them with what you want to do, which in your case means calling into external validator right?

 

 

Feb 22, 2011 at 6:31 PM

Hey Sacha and thanks for the quick reply.

My thinking behind using a NHibValidationRule was to leave your existing infrastructure in place to cover any validation that is not dependent on the mode, that only the UI would know about. You've already got a nice implementation for IDataErrorInfo, including a mechanism to fire Rule evaluation as needed, which again suggests using a rule.

I realize it make be a bit tricky to get the rule to add translated broken rules to the BrokenRuleCollection but it sounds doable.

Is there something about this approach that you don't like?

Feb 22, 2011 at 6:35 PM

Or are you suggesting I write a full replacement for your ValidatingViewModelBase?

Feb 23, 2011 at 8:39 AM

No you could do that, the only thing that would be tricky for you is to translate your external rules back into Cinch based "Rule" types to pass back on the GetBrokenRules() / GetBrokenRules(string property) methods.

 

At the end of the day I think there are 3 ways you could go

 

1. Ignore the GetBrokenRules() / GetBrokenRules(string property) methods, and simply override the IDataErrorInfo stuff to call into your validation framework

2. Keep the  IDataErrorInfo stuff in, and override the GetBrokenRules() / GetBrokenRules(string property) methods, and perform some sort of translation between NHibernate rules and CInch Rule

3. Do a complete rewrite, but that is pretty harsh, and I do not see that you need to go this far

 

At the end of the day you have options and it is up to you how you end up doing it.