Validation Rules Priority and Inheritance

Validation Rules Priority and Inheritance

Old forum URL: forums.lhotka.net/forums/t/7334.aspx


cdkisa posted on Friday, July 24, 2009

If I have a base class that has validation rules and I inherit that class and add even more validation rules, how do I know in which priority they will run?

Will the base priorities run "seperately" in order before the derived rules run in order?

Example:
Public Class MyBusinessBase(Of T As MyBusinessBase(Of T))
...
Protected Overrides Sub AddBusinessRules()
'ADD BASE VALIDATION RULES WITH PRIORITIES
ValidationRules.AddRule(AddressOf Csla.Validation.CommonRules.StringRequired, _Name, 0)
ValidationRules.AddRule(AddressOf Csla.Validation.CommonRules.StringMaxLength, New Csla.Validation.CommonRules.MaxLengthRuleArgs(_Name, 50), 1)
Sub
End Class

Public Class DerivedBusinessClass
Inherits MyBusinessBase(Of DerivedBusinessBase)
...
Protected Overrides Sub AddBusinessRules()
'ADD VALIDATION RULES WITH PRIORITIES
ValidationRules.AddRule(AddressOf Csla.Validation.CommonRules.StringRequired, _Prefix, 0)
ValidationRules.AddRule(AddressOf Csla.Validation.CommonRules.StringRequired, _Suffix, 1)
End Sub
End Class

Cheers,
Pat

JoeFallon1 replied on Friday, July 24, 2009

All the priority 0 rules run first. Then all the priority 1 rules. Then 2,3,4, etc.

The inheritance level does not make any difference. All the rules are "gathered up" first and put in priority order.

Joe

 

cdkisa replied on Friday, July 24, 2009

So I should create category ranges for the rules.

I.E.

Non-database lookups: 0 - 100
Database lookups: 101-200

Thanks for the info.

rsbaker0 replied on Friday, July 24, 2009

JoeFallon1:

All the priority 0 rules run first. Then all the priority 1 rules. Then 2,3,4, etc.


The inheritance level does not make any difference. All the rules are "gathered up" first and put in priority order.


Joe


 



Just to be clear, I'm not sure this is true when rules fire for dependent properties.

When a particular property changes, its own rules definitely execute in priority order, but I observed rules executing out of priorty order when rules fired for dependent properties.

At the tiime, I was using short-circuiting and thought I could "guarantee" that a rule of lower priority would not execute by short-circuiting an earlier rule, but this didn't work. So, I try not to depend on the order of execution being absolute.

JoeFallon1 replied on Friday, July 24, 2009

rsbaker0:
JoeFallon1:

All the priority 0 rules run first. Then all the priority 1 rules. Then 2,3,4, etc.

The inheritance level does not make any difference. All the rules are "gathered up" first and put in priority order.

Joe

 

Just to be clear, I'm not sure this is true when rules fire for dependent properties. When a particular property changes, its own rules definitely execute in priority order, but I observed rules executing out of priorty order when rules fired for dependent properties. At the tiime, I was using short-circuiting and thought I could "guarantee" that a rule of lower priority would not execute by short-circuiting an earlier rule, but this didn't work. So, I try not to depend on the order of execution being absolute.

I just reviewed the book and then the code because I believe it does work correctly for dependent properties. The descriptive text in the book describes the various overloads for CheckRules pp327-330 in the VB book. The description say that the list is always sorted in priority order. Even for dependent properties. When I looked at the code the C# method

private void CheckRules(ValidationRulesManager rules, string propertyName)

has the same body as the main method (with the exception of not calling for dependent propeties again so it is not recursive.)

I have to think that something else was happening which led you to believe it was not working correctly.

Joe

Copyright (c) Marimer LLC