{"__v":4,"_id":"554358c8b7f4540d00fcef43","category":{"__v":9,"_id":"5543585f795b590d001dc89a","pages":["554358c8b7f4540d00fcef43","55435b69b7f4540d00fcef46","55435bf2795b590d001dc8a3","55435ec262b30e0d004b1706","555636f626e9bc0d0044ea81","55563946ea5e120d00188550","555639cc26e9bc0d0044ea8d","5561dd7bb40338210035f905","56bafe8ccec63e0d00f0d2eb"],"project":"553dec691a946a0d00ad6f27","version":"553dec691a946a0d00ad6f2a","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-05-01T10:41:35.279Z","from_sync":false,"order":1,"slug":"usage","title":"Usage"},"project":"553dec691a946a0d00ad6f27","user":"54c4b05742190d0d00f5fbde","version":{"__v":2,"_id":"553dec691a946a0d00ad6f2a","project":"553dec691a946a0d00ad6f27","createdAt":"2015-04-27T07:59:37.477Z","releaseDate":"2015-04-27T07:59:37.477Z","categories":["553dec691a946a0d00ad6f2b","5543585f795b590d001dc89a"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-05-01T10:43:20.688Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":1,"body":"This library allows you to build a list of entities fluently and tersely. \n\nHere is an example:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"var customers = CustomerBuilder.CreateListOfSize(5)\\n\\t.TheFirst(1).WithFirstName(\\\"First\\\")\\n\\t.TheNext(1).WithLastName(\\\"Next Last\\\")\\n\\t.TheLast(1).WithLastName(\\\"Last Last\\\")\\n\\t.ThePrevious(2).With(b => b.WithLastName(\\\"last\\\" + (++i).ToString()))\\n\\t.All().WhoJoinedIn(1999)\\n\\t.BuildList();\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nThis would create the following (represented as json):\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"[\\n\\t{\\n\\t\\t\\\"FirstName\\\":\\\"First\\\",\\n\\t\\t\\\"LastName\\\":\\\"LastNameff51d5e5-9ce4-4710-830e-9042cfd48a8b\\\",\\n\\t\\t\\\"YearJoined\\\":1999\\n\\t},\\n\\t{\\n\\t\\t\\\"FirstName\\\":\\\"FirstName7b08da9c-8c13-47f7-abe9-09b73b935e1f\\\",\\n\\t\\t\\\"LastName\\\":\\\"Next Last\\\",\\n\\t\\t\\\"YearJoined\\\":1999\\n\\t},\\n\\t{\\n\\t\\t\\\"FirstName\\\":\\\"FirstName836d4c54-b227-4c1b-b684-de4cd940c251\\\",\\n\\t\\t\\\"LastName\\\":\\\"last1\\\",\\n\\t\\t\\\"YearJoined\\\":1999\\n\\t},\\n\\t{\\n\\t\\t\\\"FirstName\\\":\\\"FirstName5f53e895-921e-4130-8ed8-610b017f3b9b\\\",\\n\\t\\t\\\"LastName\\\":\\\"last2\\\",\\n\\t\\t\\\"YearJoined\\\":1999\\n\\t},\\n\\t{\\n\\t\\t\\\"FirstName\\\":\\\"FirstName9cf6b05f-38aa-47c1-9fd7-e3c1009cf3e4\\\",\\n\\t\\t\\\"LastName\\\":\\\"Last Last\\\",\\n\\t\\t\\\"YearJoined\\\":1999\\n\\t}\\n]\",\n      \"language\": \"json\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Castle Dynamic Proxy Generator Exception error\"\n}\n[/block]\nIf you use the list builder functionality and get the following error:\n\n> 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.\n\nThen you either need to:\n\n* Make your builder class public\n* Add the following to your `AssemblyInfo.cs` file: \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"[assembly: InternalsVisibleTo(\\\"DynamicProxyGenAssembly2\\\")]\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Non-virtual method Invalid Operation Exception\"\n}\n[/block]\nIf you use the list builder functionality and get the following error:\n\n> 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.\n\nThen 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.","excerpt":"","slug":"creating-lists","type":"basic","title":"Creating Lists of objects"}

Creating Lists of objects


This library allows you to build a list of entities fluently and tersely. Here is an example: [block:code] { "codes": [ { "code": "var customers = CustomerBuilder.CreateListOfSize(5)\n\t.TheFirst(1).WithFirstName(\"First\")\n\t.TheNext(1).WithLastName(\"Next Last\")\n\t.TheLast(1).WithLastName(\"Last Last\")\n\t.ThePrevious(2).With(b => b.WithLastName(\"last\" + (++i).ToString()))\n\t.All().WhoJoinedIn(1999)\n\t.BuildList();", "language": "csharp" } ] } [/block] This would create the following (represented as json): [block:code] { "codes": [ { "code": "[\n\t{\n\t\t\"FirstName\":\"First\",\n\t\t\"LastName\":\"LastNameff51d5e5-9ce4-4710-830e-9042cfd48a8b\",\n\t\t\"YearJoined\":1999\n\t},\n\t{\n\t\t\"FirstName\":\"FirstName7b08da9c-8c13-47f7-abe9-09b73b935e1f\",\n\t\t\"LastName\":\"Next Last\",\n\t\t\"YearJoined\":1999\n\t},\n\t{\n\t\t\"FirstName\":\"FirstName836d4c54-b227-4c1b-b684-de4cd940c251\",\n\t\t\"LastName\":\"last1\",\n\t\t\"YearJoined\":1999\n\t},\n\t{\n\t\t\"FirstName\":\"FirstName5f53e895-921e-4130-8ed8-610b017f3b9b\",\n\t\t\"LastName\":\"last2\",\n\t\t\"YearJoined\":1999\n\t},\n\t{\n\t\t\"FirstName\":\"FirstName9cf6b05f-38aa-47c1-9fd7-e3c1009cf3e4\",\n\t\t\"LastName\":\"Last Last\",\n\t\t\"YearJoined\":1999\n\t}\n]", "language": "json" } ] } [/block] [block:api-header] { "type": "basic", "title": "Castle Dynamic Proxy Generator Exception error" } [/block] 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: [block:code] { "codes": [ { "code": "[assembly: InternalsVisibleTo(\"DynamicProxyGenAssembly2\")]", "language": "csharp" } ] } [/block] [block:api-header] { "type": "basic", "title": "Non-virtual method Invalid Operation Exception" } [/block] 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.