Validation through Config should support inheritance

Topics: Validation Application Block
Sep 16, 2007 at 11:21 PM
Edited Sep 16, 2007 at 11:22 PM
Validation through attributes work well with inheritance. Let's say you have the following:

public class Base
{
[NotNullValidator] public string BaseValue;
}

public class Derived : Base
{
[NotNullValidator] public string DerivedValue;
}

You can use Validation.Validate<Derived>(derived) to validate the whole thing or Validation.Validate<Base>(derived) to validate just the Base class stuff.

However, with configuration, there is no such thing as inheritance. This means that you need to declare Base fields in the Derived section as well.

<validation>
<type name="Base...">
<ruleset name="Default">
<properties>
<property name="BaseValue"> <validator type="NotNullValidator"/></property>
</properties>
</ruleset>
</type>
<type name="Derived...">
<ruleset name="Default">
<properties>
<property name="BaseValue"> <validator type="NotNullValidator"/></property>
<property name="DerivedValue"> <validator type="NotNullValidator"/></property>
</properties>
</ruleset>
</type>
</validation>

Another way, would be to perform 2 consecutive validations, one with the Base target type, one with the Derived target type.

I wish that gap between using Config / Attributes could be solved in a future version.

Do you think on the short term, the best way to solve the gap would be to add a BaseType validator and always add it as a Type validator on each ruleset?