{"id":897,"date":"2017-10-23T12:50:33","date_gmt":"2017-10-23T11:50:33","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=897"},"modified":"2020-02-19T12:26:24","modified_gmt":"2020-02-19T11:26:24","slug":"c-builder-pattern","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-builder-pattern\/","title":{"rendered":"C# Builder Pattern"},"content":{"rendered":"\n<p>Source: <a href=\"http:\/\/blogs.tedneward.com\/patterns\/Builder-CSharp\/\">http:\/\/blogs.tedneward.com\/patterns\/Builder-CSharp\/<\/a><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class GuardedFluentBuilder\n{\n\tprivate Product product;\n\tpublic FluentBuilder Begin(string name) {\n\t\tproduct = new Product(name);\n\t\treturn this;\n\t}\n\tpublic FluentBuilder Engine {\n\t\tget \n\t\t{\n\t\t\tproduct.Parts.Add (\"Engine\");\n\t\t\treturn this;\n\t\t}\n\t}\n\tpublic FluentBuilder SteeringWheel\n\t{\n\t\tget \n\t\t{\n\t\t\tproduct.Parts.Add(\"Steering Wheel\");\n\t\t\treturn this;\n\t\t}\n\t}\n\tpublic FluentBuilder Tire() \n\t{\n\t\tproduct.Parts.Add(\"Tire\");\n\t\treturn this;\n\t}\n    public Product Build() \n    {\n        if (product.Parts.Length &lt; 4) throw new Exception(\"Product doesn't have enough parts to it yet\"); if (product.Parts.Contains(\"Engine\") == false) throw new Exception(\"Product must have an Engine\"); if (product.Parts.Find((str) => str == \"Tire\").Length &lt; 2)\n            throw new Exception(\"Product must have at least 2 Tires\");\n\n        return product;\n    }\n}\n\nvar builder = new FluentBuilder();\nproduct = builder.Begin(\"MyProduct\")\n    .Engine\n    .SteeringWheel\n    .Tire()\n    .Tire()\n    .Build();\n<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: http:\/\/blogs.tedneward.com\/patterns\/Builder-CSharp\/<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-897","post","type-post","status-publish","format-standard","hentry","category-dotnet"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/897","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=897"}],"version-history":[{"count":4,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/897\/revisions"}],"predecessor-version":[{"id":3455,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/897\/revisions\/3455"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=897"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=897"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=897"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}