Validation of items in a SortedList<string, SomeClass>

May 26, 2011 at 8:59 PM
Edited May 26, 2011 at 9:37 PM

I am not sure how to implement a specification for validation of items in a SortedList<string, SomeClass>.

I need to validate the count on a property of type SortedList<string, SomeClass> ... Done

Can you please provide an example that explains how to vallidate each intance of SomeClass in the generic sorted list?

public class SomeContainerClass
{
       public SortedList<string, SomeClass> SomeData {get;set;}
}

I ended up creating a specification for

public class SomeClassKeyValuePairSpec : Validates<KeyValuePair<string, SomeClass>>
{
}

Is there a better way?

Thanks

Coordinator
May 26, 2011 at 11:23 PM

A good place to start is the collections documentation at http://specexpress.codeplex.com/wikipage?title=Validating%20Collections. If that doesn't answer your question, let me know. I'll be happy to clear up any additional questions.

 

-Alan

May 28, 2011 at 3:36 PM

That page doesn't directly mention anything about generic sorted lists but it says that can use ForEachSpecification for a collection. Since, a sorted list is a collection of KeyValuePairs<whatever1, whatever2>, I created a specifictation for it and things seem to work and then check the specification for value in the keyvaluepair specification. I just wanted your feedback on this approach, since there is not reference for it.

Coordinator
May 28, 2011 at 4:48 PM

Sorry, I read over the KeyValuePair part too quickly. I would consider a separate Specification for KeyValuePair<string, SomeClass> to be a little too noisy and not very reusable. Unless the order of the SortedList is important to the Specification I might use Linq to just extract and convert the SortedList to IEnumerable<SomeClass>, like this:

Check( p=> p.SomeData.Select(kv => kv.Value)).Required.ForEachSpecification();

Then have a SomeClassSpecification: Validates<SomeClass>, which is a little cleaner than Validates<KeyValuePair<string, SomeClass>>, I think.

 

-Alan