简体   繁体   English

清单上的ObjectValidator <MyType>

[英]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. 我正在使用企业库验证块将验证添加到WCF服务套件中的入站数据对象中。

I have the [ObjectValidator] attribute added to my message contracts as below: 我将[ObjectValidator]属性添加到我的消息合同中,如下所示:

[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]给出如下:

[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. 我注意到的是,如果将我的MessageContract设置为List<MyType>则不会触发验证。 If I remove the List<> and use a straight MyType , then it does fire. 如果我删除List<>并使用直接的MyType ,则它将触发。

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. 显然,企业库代码不会在列表下方,而是在List对象本身中查找验证项。 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. 在列表上使用ObjectValidator时,需要改为使用ObjectCollectionValidator

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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