Property validation w/PolicyInjection

Topics: Policy Injection Application Block, Validation Application Block
Aug 22, 2007 at 2:16 AM
The validation handler doesn't work when I use Wrap with an interface. I think other people have run into this problem as I've seen posts that are similar but I want to confirm it is still a problem in EntLib 3.1. The following doesn't throw an exception on setting the Name property to a string that is too long. I do get a proxy, not the real object.. Am I doing something wrong?

using System;

using Microsoft.Practices.EnterpriseLibrary.PolicyInjection;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;

namespace PITest
class Program
static void Main( string[] args )
Thing thing = new Thing();
IThing proxy = PolicyInjection.Wrap<IThing>( thing );

proxy.Name = new string( 'x', 100 );

public interface IThing
StringLengthValidator( 1, 32, MessageTemplate = "Length invalid" )
string Name { get; set; }

public class Thing : IThing
private string _name;

StringLengthValidator( 1, 32, MessageTemplate = "Length invalid" )
public string Name
get { return _name; }
set { _name = value; }
Aug 22, 2007 at 9:48 PM
I've now tried using PolicyInjection.Create<> as well as deriving from MarshalByRefObject. [LogCallHandler] works but the validation handler never gets invoked. Using Validation.Validate<Thing>(thing) gives the proper ValidationResults. I have to be missing obvious, I can't believe this just doesn't work at all.
Aug 22, 2007 at 9:59 PM
Edited Aug 22, 2007 at 10:00 PM
Adding the following method to Thing does result in an exception as expected. The problem appears to be validating properties. I'm guessing I don't have the syntax correct for a property which the help doesn't show an example of although it is stated that property validation is supported.

public void SetName( [StringLengthValidator( 1, 32, MessageTemplate = "Length invalid" )] string name )
Name = name;
Aug 23, 2007 at 2:17 AM
Edited Aug 23, 2007 at 2:17 AM
I have a similar problem, and so do lot of people. But no reply in the forums.
Aug 23, 2007 at 3:22 AM
Edited Aug 23, 2007 at 7:08 AM

Attaching an attribute to a property, as required to validate the property's current value, is different from attaching attributes to the setter method and its parameter for the policy to validate the value being set. You'll need to duplicate the validation definition for both uses.

Something like this should work (but I haven't tried it yet - I'm not on my dev machine)

public int MyProperty
get { return myVar; }
[param: NotNullValidator]
[method: ValidationCallHandler]
set { myVar = value; }

Please post if this worked for you.

Aug 23, 2007 at 9:31 PM
Edited Aug 23, 2007 at 9:32 PM
Yes, this worked!

I needed to define the attributes on the interface, IThing, When the attributes were on Thing no checking is done. If I derive Thing from MarshalByRefObject and specify PolicyInjection.Wrap<Thing>(thing) then the attributes on Thing work.

Thanks for the response!