views:

41

answers:

2

I'm using Enterprise library Validation application block.

abstract Class A { public int fieldA; }

class B:A { }

I add validators to type B. I'm not able to find the field fieldA and also even if I enter it in configuration, the validation does not succeed. Does VAB not take the abstract class and inherited properties or fields into account?

+1  A: 

I did a test and this is working fine for base classes and abstract base classes. Can you post your configuration?

Be careful that you are not using the property validation XML when you are dealing with fields (and vice versa).

Based on your sample above, your configuration should look like:

  <validation>
    <type assemblyName="MyProject.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
      name="MyProject.Test.B">
      <ruleset name="RuleSetB">
        <fields>
          <field name="fieldA">
            <validator lowerBound="1" lowerBoundType="Inclusive" upperBound="100"
              upperBoundType="Inclusive" negated="false" messageTemplate="fieldA must be between 1 and 100"
              messageTemplateResourceName="" messageTemplateResourceType=""
              tag="" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.RangeValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null"
              name="Range Validator" />
          </field>
        </fields>
      </ruleset>
    </type>
  </validation>
Tuzo
A: 

VAB supports inheritance only when using attributes. Inheritance is not supported for configuration based validation (as you can read in the FAQ here). The work around given by the entlib team is this:

To work around this issue, you can replicate the validation specification for the subclasses.

Of course this work around sucks, because for each and every change in a base class you possibly have to copy it to many derived types. This is brittle and error prone. Because of this I built a solution that is able to duplicate validations from base classes to implementations.

It is just too much code to post it here on Stackoverflow, but you can read my blog post about it here.

I hope this helps.

Steven