Collection validation has at least one item using validation app block

Using the Enterprise Library 4.1 Validation Application block, how can I check that a collection property contains at least one item?

+2


source to share


2 answers


I assume you mean out of the box. If so, then I don't think there is a way to directly check the number of items in a collection.

Here are some other ways you might try:



  • Indicate that you are only dealing with null collections, not empty collections, and are using the Not Null Validator. Not practical, however.

  • Use self-assessment and check the object in code to make sure the collection has the correct number of items. Will work, but it's nice to have confirmation in the config file.

  • Print the collection counter as a property. This can be done by assuming, for example, a collection of employees with an EmployeeCount property on your object that contains the collection, or you can create your own collections that display the count property. Then you can use a range checker to check for the Count property.

  • Create your own validator that can check the number of items in a collection - something like CollectionCountRangeValidator.


If I wanted to develop something quickly, I would probably go with option 3. However, option 4 fits well with the Enterprise Library approach, and also allows your class design to be validated regardless of requirements. Plus, you can always use it in your next project. :) And is anyone really missing out on creating their own collections when the List is doing nicely?

+1


source


This is already implemented in the EntLib Contrib . This is called the CollectionCountValidator.



+1


source







All Articles