ObjectValidatorAttribute on paramter? - ArgumentNullException targetType

Topics: Policy Injection Application Block, Validation Application Block
May 14, 2007 at 9:54 AM
Hi,

Should it be possible to use the ObjectValidator on a parameter?

I have this public Representor StoreNewCustomer(ObjectValidator()Customer customer) and using the ValidationCallHandler from PIAB I get this when that method is called

failed: System.ArgumentNullException : Value cannot be null.
Parameter name: targetType
at Microsoft.Practices.EnterpriseLibrary.Validation.Validators.ObjectValidator..ctor(Type targetType, String targetRuleset)
at Microsoft.Practices.EnterpriseLibrary.Validation.Validators.ObjectValidatorAttribute.DoCreateValidator(Type targetType)
at Microsoft.Practices.EnterpriseLibrary.Validation.Validators.ValidatorAttribute.DoCreateValidator(Type targetType, Type ownerType, MemberValueAccessBuilder memberValueAccessBuilder)
at Microsoft.Practices.EnterpriseLibrary.Validation.Validators.ValidatorAttribute.Microsoft.Practices.EnterpriseLibrary.Validation.IValidatorDescriptor.CreateValidator(Type targetType, Type ownerType, MemberValueAccessBuilder memberValueAccessBuilder)
at Microsoft.Practices.EnterpriseLibrary.Validation.ParameterValidatorFactory.CreateValidator(ParameterInfo paramInfo)
at Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.ValidationCallHandler.CreateValidator(ParameterInfo parameter)
at Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.ValidationCallHandler.Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
at Microsoft.Practices.EnterpriseLibrary.PolicyInjection.HandlerPipeline.Invoke(IMethodInvocation input, InvokeHandlerDelegate target)
at Microsoft.Practices.EnterpriseLibrary.PolicyInjection.RemotingInterception.InterceptingRealProxy.Invoke(IMessage msg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at Northgate.LandProperty.Ldf.BusinessLogic.LdfManager.StoreNewCustomer(Customer customer)
F:\work\dataserver\NET2005\WebServices\LdfWebService\LdfWebServiceUnitTests\LdfManagerTests.cs(74,0): at LdfWebServiceUnitTests.LdfManagerTests.StoreCustomer()

I'm using all attributes for validation and the PIAB and the method is on a class that derives from MarshalByRefObject


The customer object has a string length validator on one property

Is this a bug or am I missing something

Thanks
Richard
May 14, 2007 at 4:50 PM
You don't need the ObjectValidatorAttribute.

public class MyClass : MarshalByRefObject
{
    [ValidationCallHandler]
    public void StoreNewCustomer(Customer customer)
    {
        // Do Something...
    }
}

The ValidationCallHandlerAttribute will be enough for validation to occur on the Customer instance when you do:

MyClass myClass = PolicyInjection.Create<MyClass>();
 
try
{
    myClass.StoreNewCustomer(customer);
    // ...
}
catch(ArgumentValidationException ex)
{
    // ...
}

Regards,

Dave

________________________________

David Hayden
Microsoft MVP C#