Lists

One of NBuilder's most useful features is its ability to create lists.

NBuilder has two different property namers, a sequential namer and a random namer. The default namer is the sequential one. This is probably the most useful for most scenarios because you know exactly what it is going to produce.

It always starts from one and gives ascending values to the properties for each object it builds.

Creating a basic list

NBuilder will name all public properties and fields but it won't touch private, protected or internal ones.

It will create a list whose properties are named like this:

Using WhereAll()

You can specify that different parts of the list have certain property values. These are called 'declarations'. The most basic declaration is WhereAll(). When you declare a set of objects, you always follow it with what you want to do to that set of objects. In this case Have() is being used. Have is the same as the single object builder's With() method.

One common use for WhereAll() is if you want to insert a load of objects into the database. Most ORMs will treat an object with an ID of 0 as a new object so to do this you would say

WhereAll().Have(x => x.Id = 0).

WhereTheFirst(), WhereTheLast()

There are some methods provided to quickly declare the first or last x objects.

All these methods (apart from WhereAll()) are in fact extension methods. It follows then that you can add your own WhereXXX methods to NBuilder, simply by writing an extension method.

AndTheNext(), AndThePrevious()

When you use WhereTheFirst() you can follow it with multiple calls to AndTheNext()

As you might expect there's also a WhereTheLast() and AndThePrevious as well

WhereSection(x, y)

You can use WhereSection() to apply rules to segments of the list

Calling a method on the objects of a declaration

If you want to call a method on a set of objects, there are the HaveDoneToThem() and HasDoneToIt() methods

Picking random items

If you want to pick an item or some items at random, and you don't care which they are, you can use the Pick class.

You can also pick a unique random list using the Pick class