Validation using Policy Injection - using config file

Topics: Policy Injection Application Block, Validation Application Block
May 6, 2010 at 12:22 PM
Edited May 6, 2010 at 12:31 PM

I have been trying to do a property validation using Policy injection, however the validation doesn't work. Can you please suggest what could be wrong here?

My app.config is as under:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="validation" type="Microsoft.Practices.EnterpriseLibrary.Validation.Configuration.ValidationSettings, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    <section name="policyInjection" type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.Configuration.PolicyInjectionSettings, Microsoft.Practices.EnterpriseLibrary.PolicyInjection, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
  </configSections>


  <validation>
    <type name="VabPolicyInjection.Customer" defaultRuleset="ValidationRuleset"
      assemblyName="VabPolicyInjection, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
      <ruleset name="ValidationRuleset">
        <properties>
          <property name="Name">
            <validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.StringLengthValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
              upperBound="10" messageTemplate="Should be 10 chars" name="StringLengthValidator" />
          </property>
        </properties>
      </ruleset>
    </type>
  </validation>
  <policyInjection>
    <policies>
      <add name="policy">
        <matchingRules>
          <add type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.MatchingRules.PropertyMatchingRule, Microsoft.Practices.EnterpriseLibrary.PolicyInjection, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            name="PropertyMatchingRule">
            <matches>
              <add matchOption="Set" match="Name" />
            </matches>
          </add>
        </matchingRules>
        <handlers>
          <add type="Microsoft.Practices.EnterpriseLibrary.Validation.PolicyInjection.ValidationCallHandler, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            ruleSet="ValidationRuleset" specificationSource="Configuration"
            name="ValidationCallHandler" />
        </handlers>
      </add>
    </policies>
  </policyInjection>
</configuration>

While my Customer.cs is as under:

using System;
using System.Text;

namespace VabPolicyInjection
{
    public class Customer : MarshalByRefObject
    {
        public Customer()
        {

        }
        public Customer(string value)
        {
            Name = value;
        }
        public string Name { get; set; }

        public bool Save()
        {
            return true;
        }
    }
}

And my main Program.cs is as under:

using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection;

namespace VabPolicyInjection
{
    class Program
    {
        static void Main(string[] args)
        {
            try {

                //Customer customer = new Customer();
                Customer customer = PolicyInjection.Create<Customer>();
                customer.Name = "Devendra Singh";
                customer.Save();

            }
            catch (Exception ex)
            {

                string error = string.Empty;
                error = ex.Message;
                //foreach (ValidationResult result in ex.ValidationResults)

                //    error += result.Message + ".  ";

            }
}
}
}

May 7, 2010 at 1:50 AM

You misunderstood how it works.  When you configure a PropertyMatchingRule, the type of the property whose get/set accessor you want to intercept should match the type to which the RuleSet you specified in the ValidationCallHandler is configured for.  To explain it clearly, in your case, you specified the "ValidationRuleSet" which is a rule set for the Customer type but you want to intercept the Name propert which is string type.  One way to make it work is to configure a rule set for the string type:

 

<type name="System.String" defaultRuleset="StringLengthRuleSet"
   assemblyName="mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
       <ruleset name="StringLengthRuleSet">
             <methods>
                  <method name="ToString">
                     <validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.StringLengthValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                       upperBound="10" messageTemplate="Should be 10 chars" name="String Length Validator" />
                  </method>
             </methods>
      </ruleset>
</type>

Then specify "StringLengthRuleSet" as the RuleSet for the ValidationCallHandler.

 

Sarah B. Urmeneta   
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com

May 7, 2010 at 5:00 AM

Thanks Sarah, this works fine for what I have been trying to do.

Just for my knowledge, if possible can you please post a scenario using the property ruleset?

May 7, 2010 at 7:08 AM

I don't think I understand what you want - "if possible can you please post a scenario using the property ruleset?"

 

Sarah B. Urmeneta   
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com

May 7, 2010 at 7:21 AM

I was thinking where exactly or under what circumstances and how to use the property ruleset?

OR

When should one use the property ruleset?

Since it is provided by Enterprise Library, I guess it should have some use somewhere....

May 7, 2010 at 7:28 AM

Ok, what I'm not getting is what are you referring to when you say "property ruleset"?  =

 

Sarah B. Urmeneta   
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com

May 7, 2010 at 7:34 AM

We worked this out using Method validator i.e.

<ruleset name="StringLengthRuleSet">
             <methods>
                  <method name="ToString">
                     <validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.StringLengthValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                       upperBound="10" messageTemplate="Should be 10 chars" name="String Length Validator" />
                  </method>
             </methods>
      </ruleset>

I wanted to know when to use

<ruleset name="ValidationRuleset">
        <properties>
          <property name="Property To Validate" />

             ..........


        </properties>
      </ruleset>

May 7, 2010 at 8:05 AM

It's used to define validation rules on class properties.  We assigned validator to the method because we're specifying validation rule for the String type and the String type has no other properties aside from the Length property.

In addition, we had to do that because you used the PropertyMatchingRule and wanted to validate upon setting the Name property. 

If you don't want to validate upon setting the value of the property but wanted to validate an object of type Customer, setting validators on the properties of the customer class is useful.

Customer customer = new Customer();

customer.Name = "This is an invalid name.";

ValidationResults results = Validation.Validate(customer);

Sarah B. Urmeneta   
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com

May 7, 2010 at 9:20 AM

Ok. Thanks...