Object Collection Validator

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.

Class Name: ObjectCollectionValidator

Attribute Name: ObjectCollectionValidatorAttribute

Configuration tool name: Object Collection Validator

Description

This validator checks that the object is a collection of the given type and then invokes validation on each element of the collection. If the object you want to validate is null, the validation is ignored. If the object you want to validate is not a collection, then the validation fails and the rule set is not applied. If there are elements in the collection that are of a different type then the one you specified for the object, then the validation for these elements fails but this does not affect validation for the other elements.

Properties

The following table lists the Object Collection Validator properties.

Object Collection Validator

Property

Description

Tag

This property is a user-supplied string. Typically, it is used to sort or categorize validation results that are in a log.

TargetRuleset

This is the name of the rule set that will be applied to each element in the collection.

TargetType

This property is the type of the object that you want to validate. You can either enter the type or you can select it with the Type Selector – System.Object dialog box.

MessageTemplate

This property is a user-supplied string. Typically, it describes the validation result and is intended for a log.

MessageTemplateResourceName

If you do not want to use the MessageTemplate property to specify a message, you can use another template by specifying it with the MessageTemplateResourceName value. If you include both a MessageTemplate value and a MessageTemplateResourceName value, the MessageTemplate value takes precedence.

MessageTemplateResourceTypeName

If you specify a MessageTemplateResourceName value then you must specify a MessageTemplateResourceTypeName value, which is the type of the template you want to use.

Examples

The following examples show how to use the validator with attributes.

Attributes

The following code example checks that each part in a kit's part list meets the requirements of the Part type's validator. The Part type's validator checks that the name is fewer than 20 characters and that the manufacturer's URL is well formed according to a particular regular expression pattern.

public class Part
{
   [StringLengthValidator(20)]
   string name;
   [RegexValidator("http://(www\.)?([^\.]+)\.com")]
   string manufacturerUrl;
   // ...
}
public class Kit
{
   [ObjectCollectionValidator(typeof(Part)]
   public Part[] PartList
   {
      get
      {
         return partList;
      }
   }
   // ...
}
'Usage
Public Class Part
    <StringLengthValidator(20)> _
    Dim name As String
    <RegexValidator("http://(www\.)?([^\.]+)\.com")> _
    Dim manufacturerUrl As String
    ' ...
End Class
Public Class Kit
    <ObjectCollectionValidator(GetType(Part))> _
    ReadOnly Property PartList(ByVal _partList As String)
        Get
            Return _partList
        End Get
    End Property
' ...
End Class
Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.