Validation: Selecting Generic Types in Config GUI and Collection Types

Topics: Validation Application Block
Jul 26, 2010 at 5:43 PM

I have a class, the relevant definition of which is:

Public Class TopicList(Of T)
    Inherits BusinessObjectListBase(Of Topic)

    Property Experience As ExperienceBase(Of T)

    'Other code here

End Class

Public MustInherit Class BusinessObjectListBase(Of T)
    Inherits Generic.List(Of T)
    Implements IDataErrorInfo

    Property ValidationResults As ValidationResults

    Protected ReadOnly Property ValidatorFactory As ValidatorFactory
            Return EnterpriseLibraryContainer.Current.GetInstance(Of ValidatorFactory)()
        End Get
    End Property

    Protected Function Validate() As Boolean
        Dim validator As validator = ValidatorFactory.CreateValidator(Me.GetType)
        _ValidationResults = validator.Validate(Me)
        Return _ValidationResults.IsValid
    End Function

    'Other code here
End Class

Public Class Topic
    Property Id As Integer
    Property Value As String
End Class

I want the list class to be able to validate itself, specifically that each topic in the list has a value whose string length is 3-100 characters.

  The approach I was going to take was to:

  • Put a collection validator on the GetEnumerator method of TopicList(of T), with a target type of Topic
  • Put a string length validator on Topic.Value

When I go to select the TopicList'1 type in the Enterprise Library Configuration gui, I get the error message:

There was an error resolving the type: Type argument not set for parameter T

So my two questions are:

  • Is it possible to make a collection self validating and if so, how?  Is my approach of placing a validator on the GetEnumerator method going to work?  The collection object itself is "saveable" so it needs to validate itself.
  • How can I select a generic type as a type in the validation configuration GUI?

Thank you,



Jul 28, 2010 at 6:28 AM

You can't select a generic type using the configuration tool.  You can edit the config manually though. Example:


However, supplying this as the type attribute for a type to validate won't be that helpful because for configuration, validators are created based on the actual type.  To explain it better, supposing you instantiate a class that inherits from the BusinessObjectListBase,

Dim list As BusinessObjectListBase(Of Topic)
list = New TopicList(Of Topic)

If you're using versions prior to 5.0, if you're validating the list variable, it will be validated against validation rules defined for BusinesObjectListBase(Of Topic) type which you don't have since you only have validation rule defined for BusinessObjectListBase(Of T).  Thus, in this case, you would need to define validation rules for every possible BusinessObjectListBase.  On the other hand, if you're using version 5.0, the list variable will be validated against validation rules defined for TopicList(Of Topic) (this is one of the changes made in VAB in EntLib 5.0).  The workaround for both is to define the validators using attributes. 

You can have the collection validate each item in the list by decorating the class with the ObjectCollectionValidator attribute.  However, I'm not sure what do you mean when you say self-validate.  Do you want something like when you add an item to the collection, it will automatically detect whether that item is valid or invalid?  If that's the case, you may use policy injection or unity to intercept the Add method.  If you need further details on this, just let me know.  It would also help if you indicate what version you are using.


Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.