开发者

EntLib Validation problem on GetType(object) - expects string not object?

开发者 https://www.devze.com 2022-12-19 16:24 出处:网络
I have an Address object that I am trying to validate data against using EntLib: Given the following method:

I have an Address object that I am trying to validate data against using EntLib:

Given the following method:

<ValidatorComposition(开发者_运维百科CompositionType.And, Ruleset:="FraudAnalysis")> _
<NotNullValidator(MessageTemplate:="Billing address is required.", Ruleset:="FraudAnalysis")> _
<TypeConversionValidator(GetType(Address), MessageTemplate:="Billing address must be an address object.", Ruleset:="FraudAnalysis")> _
Public Property BillingAddress() As Address
    Get
        Return _BillingAddress
    End Get
    Set(ByVal value As Address)
        _BillingAddress = value
    End Set
End Property

I create an address object:

            Address thisAddress = new Address();
            thisAddress.Address1 = "12312 Long Street";
            thisAddress.City = "Los Angeles";
            thisAddress.State = "CA";
            thisAddress.Zip = "93322";
            // set billing address to address
            cardX.BillingAddress = thisAddress;

So now at cardX.billingAddress = thisAddress, the BillingAddress property validator (GetType(Address)) should fire. It seems to fire, but returns this error:

Value to validate is not of the expected type: expected System.String but got Address instead.

Can anyone see the issue here / suggest a fix?

Thanks.


I would just get rid of the ValidatorComposition and TypeConversionValidator declarations as I think they are redundant here. That would get rid of your error and a couple of lines of code too.

The property is already strongly-typed to the Address class so there is no way you can set it in code to an object that isn't an Address or isn't polymorphic with Address - having an extra validator to check this is redundant.

The default composition of validators is logical AND anyway, you only need to specify validator composition when you want to OR a group, or use more complex groups that combine AND / OR.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号