Validating Method Parameters Using Configuration

Topics: Policy Injection Application Block, Validation Application Block
Feb 7, 2008 at 10:19 PM
I am attempting to implement policy injection at the business logic layer of my service to do logging, exception handling, and mainly validation. I am running into a problem because I was planning on handling all validation through configuration files and not attributes.

When creating the validation configuration I seem to be stuck on figuring out a way to validate parameters of a method. I have a class with several methods on it which has been created by calling PolicyInjection.Create<BusinessLogicClass>();. So I would imagine that in configuration I should be able to select the type BusinessLogicClass and then for each method in the class; be able to put rules on the parameters.

I know that I can put rules on the parameters which are types of their own, but I have found no way to do it for primitive types such as int that is a parameter on a method.

Is there a way to put rules on primitive parameters on methods or can this only be done using attributes?
Jun 14, 2008 at 11:48 PM
Edited Jun 15, 2008 at 1:17 AM

Hi, has anyone figured this out?

Attributes are convenient for code samples, but I would imagine that most developers would prefer to use the configuration file.

Jun 16, 2008 at 2:48 PM


While there is no convenient way to specify validation for intercepted methods' parameters in a centralized fashion, you can use different validation rulesets and attach them to the parameter types even if they are not owned by you.

Here's a sample application

    8 namespace Test.Application

    9 {

   10     class Program

   11     {

   12         static void Main(string[] args)

   13         {

   14             DomainClass dc = PolicyInjection.Create<DomainClass>();


   16             DomainClass2 dc2 = new DomainClass2();

   17             dc2.Property = "aaaaabaa";

   18             dc.DoSomething(dc2, 200);

   19         }

   20     }


   22     public class DomainClass : MarshalByRefObject

   23     {

   24         public virtual void DoSomething(DomainClass2 arg1, int arg2)

   25         {

   26         }

   27     }


   29     public class DomainClass2

   30     {

   31         public string Property { get; set; }

   32     }

   33 }

With the matching configuration for PIAB, specifying that validation is to be used for a given method using a specific rule set

   33   <policyInjection>

   34     <policies>

   35       <add name="Policy">

   36         <matchingRules>

   37           <add type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.MatchingRules.MemberNameMatchingRule, Microsoft.Practices.EnterpriseLibrary.PolicyInjection, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

   38             name="Member Name Matching Rule">

   39             <matches>

   40               <add match="DoSomething" ignoreCase="false" />

   41             </matches>

   42           </add>

   43         </matchingRules>

   44         <handlers>

   45           <add ruleSet="DoSomething ruleset" specificationSource="Configuration"

   46             order="0" type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.ValidationCallHandler, Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

   47             name="Validation Handler" />

   48         </handlers>

   49       </add>

   50     </policies>

   51   </policyInjection>

And configuration for the validation block with validators on the parameter types for the handler's ruleset

    8   <validation>

    9     <type assemblyName="Test.Application, Version=, Culture=neutral, PublicKeyToken=null"

   10       name="Test.Application.DomainClass2">

   11       <ruleset name="DoSomething ruleset">

   12         <properties>

   13           <property name="Property">

   14             <validator pattern="^a*$" options="None" patternResourceName=""

   15               patternResourceType="" messageTemplate="" messageTemplateResourceName=""

   16               messageTemplateResourceType="" tag="" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.RegexValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

   17               name="Regex Validator" />

   18           </property>

   19         </properties>

   20       </ruleset>

   21     </type>

   22     <type assemblyName="mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089"

   23       name="System.Int32">

   24       <ruleset name="DoSomething ruleset">

   25         <validator lowerBound="0" lowerBoundType="Ignore" upperBound="10"

   26           upperBoundType="Inclusive" negated="false" messageTemplate=""

   27           messageTemplateResourceName="" messageTemplateResourceType=""

   28           tag="" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.RangeValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

   29           name="Range Validator" />

   30       </ruleset>

   31     </type>

   32   </validation>