Picking list items

The Pick class allows you to select items from a list according to different strategies.

When using builders to create lists of items it is often required to add values to a property that are selected from a secondary list of possible values.

Dossier provides this functionality through the Pick class, which has static methods that return different Dossier data sources based on the strategy that you want to implement. Data sources have a Data property, which is the list of data for the specified type, and a Next method, which returns the next item from the list according to the strategy the data source implements.

RandomItemFrom

Allows you to select random items from a secondary list.

var addresses = Builder<Address>.CreateListOfSize(15).BuildList();
var customers = Builder<Customer>
    .CreateListOfSize(15)
    .All()
    .Set(x => x.PostalAddress, Pick.RandomItemFrom(addresses).Next)
    .BuildList();

Note, that Next is passed as a func - as opposed to with the brackets, such as Next() - so that it will be called each time an item in the list is constructed and produce a different value for each item. If you added the brackets then Next would only be executed once and that same value would be applied to every item in the list.

For example, a list of days of the week might return these values:

Thursday
Saturday
Saturday
Wednesday
Thursday
Friday
Monday
Sunday
Thursday
Monday

Note that the list is random and some items in the list might not get used.

RepeatingSequenceFrom

Allows you to select items from a list in order. Once the list is complete it will go back to the start and continue to select items from the list in order.

var addresses = Builder<Address>.CreateListOfSize(3).BuildList();
var customers = Builder<Customer>
    .CreateListOfSize(9)
    .All()
    .Set(x => x.PostalAddress, Pick.RepeatingSequenceFrom(addresses).Next)
    .BuildList();

For example, a list of seasons would return these values:

Spring
Summer
Autumn
Winter
Spring
Summer
Autumn
Winter
Spring
Summer

Did this page help you?