{"__v":4,"_id":"555636f626e9bc0d0044ea81","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-15T18:12:06.750Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"auth":"required","params":[],"url":""},"isReference":false,"order":3,"body":"If you are building domain entities, or other important classes, having a custom builder class with intention-revealing method (e.g. WithFirstName) provides terseness (avoiding lambda expressions) and allows the builder class to start forming documentation about the usage of that object.\n\nSometimes though, you just want to build a class without that ceremony. Typically, we find that this applies for view models and DTOs.\n\nIn that instance you can use the generic `Builder` implementation as shown below:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"StudentViewModel vm = Builder<StudentViewModel>.CreateNew()\\n\\t.Set(x => x.FirstName, \\\"Pi\\\")\\n\\t.Set(x => x.LastName, \\\"Lanningham\\\")\\n\\t.Set(x => x.EnrollmentDate, new DateTime(2000, 1, 1));\\n\\nvar studentViewModels = Builder<StudentViewModel>.CreateListOfSize(5)\\n\\t.TheFirst(1).Set(x => x.FirstName, \\\"First\\\")\\n\\t.TheNext(1).Set(x => x.LastName, \\\"Next Last\\\")\\n\\t.TheLast(1).Set(x => x.LastName, \\\"Last Last\\\")\\n\\t.ThePrevious(2).With(b => b.Set(x => x.LastName, \\\"last\\\" + (++i).ToString()))\\n\\t.All().Set(x => x.EnrollmentDate, _enrollmentDate)\\n\\t.BuildList();\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nThe syntax is modelled closely against what NBuilder provides and the behaviour of the class should be very similar.\n\nNote, that in the first example above, it was not necessary to call the `Build` method at the end of the method chain. This is because the `vm` variable has been defined as `StudentViewModel` and the C# compiler is able to infer the type and the object is set *implicitly*.\n\nIn the second example, the `var` keyword is used to define `studentViewModels`, and so it is necessary to *explicitly* call `BuildList` to set the variable.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Customising the construction of the object\"\n}\n[/block]\nBy default, the longest constructor of the class you specify will be called and then all properties (with public and private setters) will be set with values you specified (or anonymous values if none were specified).\n\nSometimes you might not want this behaviour, in which case you can specify a custom construction factory (see *Build objects without calling constructor* section for explanation of factories) as shown below:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"var dto = Builder<MixedAccessibilityDto>\\n\\t.CreateNew(new CallConstructorFactory())\\n\\t.Build();\\n\\nvar dtos = MixedAccessibilityDto dto = Builder<MixedAccessibilityDto>\\n\\t.CreateListOfSize(5, new CallConstructorFactory())\\n\\t.BuildList();\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Propagating anonymous value fixture\"\n}\n[/block]\nSee the documentation about the [SetUsingBuilder method](doc:propagating-the-anonymous-value-fixture-across-bui).","excerpt":"","slug":"create-object-without-requiring-custom-builder-cla","type":"basic","title":"Generic builders"}
If you are building domain entities, or other important classes, having a custom builder class with intention-revealing method (e.g. WithFirstName) provides terseness (avoiding lambda expressions) and allows the builder class to start forming documentation about the usage of that object. Sometimes though, you just want to build a class without that ceremony. Typically, we find that this applies for view models and DTOs. In that instance you can use the generic `Builder` implementation as shown below: [block:code] { "codes": [ { "code": "StudentViewModel vm = Builder<StudentViewModel>.CreateNew()\n\t.Set(x => x.FirstName, \"Pi\")\n\t.Set(x => x.LastName, \"Lanningham\")\n\t.Set(x => x.EnrollmentDate, new DateTime(2000, 1, 1));\n\nvar studentViewModels = Builder<StudentViewModel>.CreateListOfSize(5)\n\t.TheFirst(1).Set(x => x.FirstName, \"First\")\n\t.TheNext(1).Set(x => x.LastName, \"Next Last\")\n\t.TheLast(1).Set(x => x.LastName, \"Last Last\")\n\t.ThePrevious(2).With(b => b.Set(x => x.LastName, \"last\" + (++i).ToString()))\n\t.All().Set(x => x.EnrollmentDate, _enrollmentDate)\n\t.BuildList();", "language": "csharp" } ] } [/block] The syntax is modelled closely against what NBuilder provides and the behaviour of the class should be very similar. Note, that in the first example above, it was not necessary to call the `Build` method at the end of the method chain. This is because the `vm` variable has been defined as `StudentViewModel` and the C# compiler is able to infer the type and the object is set *implicitly*. In the second example, the `var` keyword is used to define `studentViewModels`, and so it is necessary to *explicitly* call `BuildList` to set the variable. [block:api-header] { "type": "basic", "title": "Customising the construction of the object" } [/block] By default, the longest constructor of the class you specify will be called and then all properties (with public and private setters) will be set with values you specified (or anonymous values if none were specified). Sometimes you might not want this behaviour, in which case you can specify a custom construction factory (see *Build objects without calling constructor* section for explanation of factories) as shown below: [block:code] { "codes": [ { "code": "var dto = Builder<MixedAccessibilityDto>\n\t.CreateNew(new CallConstructorFactory())\n\t.Build();\n\nvar dtos = MixedAccessibilityDto dto = Builder<MixedAccessibilityDto>\n\t.CreateListOfSize(5, new CallConstructorFactory())\n\t.BuildList();", "language": "csharp" } ] } [/block] [block:api-header] { "type": "basic", "title": "Propagating anonymous value fixture" } [/block] See the documentation about the [SetUsingBuilder method](doc:propagating-the-anonymous-value-fixture-across-bui).