Creating Lists of objects

This library allows you to build a list of entities fluently and tersely.

Here is an example:

var customers = CustomerBuilder.CreateListOfSize(5)
    .TheFirst(1).WithFirstName("First")
    .TheNext(1).WithLastName("Next Last")
    .TheLast(1).WithLastName("Last Last")
    .ThePrevious(2).With(b => b.WithLastName("last" + (++i).ToString()))
    .All().WhoJoinedIn(1999)
    .BuildList();

This would create the following (represented as json):

[
    {
        "FirstName":"First",
        "LastName":"LastNameff51d5e5-9ce4-4710-830e-9042cfd48a8b",
        "YearJoined":1999
    },
    {
        "FirstName":"FirstName7b08da9c-8c13-47f7-abe9-09b73b935e1f",
        "LastName":"Next Last",
        "YearJoined":1999
    },
    {
        "FirstName":"FirstName836d4c54-b227-4c1b-b684-de4cd940c251",
        "LastName":"last1",
        "YearJoined":1999
    },
    {
        "FirstName":"FirstName5f53e895-921e-4130-8ed8-610b017f3b9b",
        "LastName":"last2",
        "YearJoined":1999
    },
    {
        "FirstName":"FirstName9cf6b05f-38aa-47c1-9fd7-e3c1009cf3e4",
        "LastName":"Last Last",
        "YearJoined":1999
    }
]

Castle Dynamic Proxy Generator Exception error

If you use the list builder functionality and get the following error:

Castle.DynamicProxy.Generators.GeneratorExceptionCan not create proxy for type <YOUR_BUILDER_CLASS> because it is not accessible. Make it public, or internal and mark your assembly with [assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")] attribute, because assembly <YOUR_TEST_ASSEMBLY> is not strong-named.

Then you either need to:

  • Make your builder class public
  • Add the following to your AssemblyInfo.cs file:
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]

Non-virtual method Invalid Operation Exception

If you use the list builder functionality and get the following error:

System.InvalidOperationException: Tried to build a list with a builder who has non-virtual method. Please make <METHOD_NAME> on type <YOUR_BUILDER_CLASS> virtual.

Then you need to mark all the public methods on your builder as virtual. This is because we are using Castle Dynamic Proxy to generate lists and it can't intercept non-virtual methods.


Did this page help you?