简体   繁体   中英

ObjectValidator on a List<MyType>

I'm using the Enterprise Library validation block to add validation to my inbound data objects within a suite of WCF services.

I have the [ObjectValidator] attribute added to my message contracts as below:

[MessageContract]
public class MyRequest
{
    public MyRequest()
    {
        Header = new MyHeader();
        Data = new List<MyType>();
    }

    [ObjectValidator]
    [MessageHeader]
    public MyHeader Header { get; set; }

    [ObjectValidator]
    [MessageBodyMember]
    public List<MyType> Data { get; set; }
}

and my [DataContract] for the body is given as this:

[DataContract]
public class MyType
{
    [Required, DataMember(IsRequired = true)]
    [RegularExpressionAttribute(RegexValidation.Name)]
    public string Name { get; set; }

    [Required, DataMember(IsRequired = true)]
    [RegularExpressionAttribute(RegexValidation.Password)]
    public string Password { get; set; }    
} 

What I notice is that if my MessageContract is setup as a List<MyType> then the validation doesn't fire. If I remove the List<> and use a straight MyType , then it does fire.

Its clear that the Enterprise Library code is not going down below the list, and is instead looking for validation items in the List object itself. Does anyone know how to get around this?

Simple answer really. When using the ObjectValidator on a list, you need to use the ObjectCollectionValidator instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM