views:

85

answers:

2

When I read rules from a configuration file, I do something like this:

IConfigurationSource configurationSource = new FileConfigurationSource("myvalidation.config");
var validator = ValidationFactory.CreateValidator<Salary>(configurationSource);

The config file looks like this:

<ruleset name="Default">
  <properties>
    <property name="Address">
       <validator lowerBound="10" lowerBoundType="Inclusive" upperBound="15"
            upperBoundType="Inclusive" negated="false" messageTemplate=""
            messageTemplateResourceName="" messageTemplateResourceType=""
            tag="" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.StringLengthValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null"
name="String Length Validator" />
     </property>
  </properties>
</ruleset>

My question is - is there a way to not specify the ruleset name? It's not required for me to specify a ruleset name if I am using the attribute approach and I can validate using:

ValidationResults results = Validation.Validate(salary);

Now when reading from config files, I have to specify the ruleset name. There is no overload of the CreateValidator method that accepts the configuration source without specifying the ruleset name. Also, the xml in the config file requires a name attribute for ruleset

+1  A: 

When using attributes, and a Ruleset is not specified in the validator or validation call all rules will be applied that do not have a Ruleset defined or the Ruleset name is empty.

However with configuration it is slightly different since (as you mention) the name of the Ruleset is required for configuration. What you need to do is to create a Ruleset with a name and then indicate that it is the default Ruleset. To indicate that a Ruleset is the default Ruleset apply the attribute defaultRuleset="MyRule" to the type element in the config.

When a validator is created with no Ruleset specified then the default Ruleset from configuration will be executed when Validate is called.

In your example the config would look like:

<type defaultRuleset="Default" [...] >
  <ruleset name="Default">
    <properties>
      <property name="Address">
         <validator lowerBound="10" lowerBoundType="Inclusive" upperBound="15"
            upperBoundType="Inclusive" negated="false" messageTemplate=""
            messageTemplateResourceName="" messageTemplateResourceType=""
            tag=""   type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.StringLengthValidator,   Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.1.0.0, Culture=neutral,   PublicKeyToken=null"
            name="String Length Validator" />
       </property>
    </properties>
  </ruleset>
</type>
Tuzo
A: 

The answer above got me to where I needed to go. However, I think the below information is also helpful.

    Validator<MyConcreteClass> validator= ValidationFactory.CreateValidatorFromConfiguration<MyConcreteClass>();
    ValidationResults vResults = validator.Validate(myInstanceOfMyConcreteClass);

From: http://msdn.microsoft.com/en-us/library/ee760419.aspx

CreateValidatorFromConfiguration<(Of <(T>)>)()()()
Returns a validator representing the validation criteria specified for type T through configuration for the default ruleset.

And also:

http://msdn.microsoft.com/en-us/library/microsoft.practices.enterpriselibrary.validation.validationfactory.createvalidator.aspx

CreateValidator<(Of <(T>)>)()()()
Returns a validator representing the validation criteria specified for type T through configuration and attributes on type T and its ancestors for the default ruleset.

Which means at the end of the day...... know that ValidationFactory !

granadaCoder