{"id":3571,"date":"2016-01-31T22:28:44","date_gmt":"2016-01-31T22:28:44","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=3571"},"modified":"2016-01-31T22:28:44","modified_gmt":"2016-01-31T22:28:44","slug":"populating-random-data","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/populating-random-data\/","title":{"rendered":"Populating random data"},"content":{"rendered":"<p>I was working on a small utility which generates XML based upon a given class (which is already XML serializable). I wanted to generate random data just so I could see how the end XML looked in case I needed to tweak the XSD. <\/p>\n<p><em>Source for the utility is available on <a href=\"https:\/\/github.com\/putridparrot\/AutoGenXml\" target=\"_blank\">AutoGenXml<\/a>.<\/em><\/p>\n<p>I figured somebody must have already approached such a problem, and thankfully I was right. There are a few solutions for populating object data. I ended up trying out two different libraries, AutoFixture and NBuilder. <\/p>\n<p><em>Disclaimer: I have literally only just started using these libraries, so I&#8217;ve yet to find all the good and\/or bad points of each library.<\/em><\/p>\n<p>Let&#8217;s take a real quick look at what these libraries can do.<\/p>\n<p><strong>Test object<\/strong><\/p>\n<p>Let&#8217;s start out by defining an object hierarchy to test this two libraries out on. Mine looks like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class Album\r\n{\r\n   public string Title { get; set; }\r\n   public string RecordLabel { get; set; }\r\n   public Artist Artist { get; set; }\r\n   public string Genre { get; set; }\r\n}\r\n\r\npublic class Artist\r\n{\r\n   public string Name { get; set; }\r\n   public BandMember&#x5B;] Band { get; set; }\r\n}\r\n\r\npublic class BandMember\r\n{\r\n   public string FirstName { get; set; }\r\n   public string LastName { get; set; }\r\n   public string Talent { get; set; }\r\n}\r\n<\/pre>\n<p><strong>AutoFixture<\/strong><\/p>\n<p>AutoFixture source can be found on <a href=\"https:\/\/github.com\/AutoFixture\" target=\"_blank\">AutoFixture<\/a>.<\/p>\n<p>We can create objects with AutoFixture (hence using it as the factory for our objects) and it returns a populated object hierarchy.<\/p>\n<p>Let&#8217;s look at the code (it&#8217;s pretty simple)<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar fixture = new Fixture();\r\nvar album = fixture.Create&lt;Album&gt;();\r\n<\/pre>\n<p>The <em>album<\/em> will now have data in all fields and the Artist and BandMember properties are also created and data supplied. <\/p>\n<p>Whilst it&#8217;d obviously be easy enough for us to create an object multiple times if we wanted a list of Albums but AutoFixture also supplies this code to do the same thing<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar fixture = new Fixture {RepeatCount = 10};\r\nvar albums = fixture.\r\n                 Repeat(fixture.Create&lt;Album&gt;).\r\n                 ToArray();\r\n<\/pre>\n<p><strong>NBuilder<\/strong><\/p>\n<p>NBuilder source can be found on <a href=\"https:\/\/github.com\/garethdown44\/nbuilder target=\"_blank\">NBuilder<\/a>.<\/p>\n<p>NBuilder also supplies a factory pattern for both creating our objects and populating the object. <\/p>\n<p>Here&#8217;s the code<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar album = Builder&lt;Album&gt;\r\n               .CreateNew()\r\n               .Build();\r\n<\/pre>\n<p>NBuilder uses a fluent style interface and offers options for creating multiple items (i.e. an IList<> of objects). There&#8217;s also a mechanism for us to intercept the object population step and supply our own data. So whilst in the usage shown above, we don&#8217;t have the object heriarchy created, we can create this ourselves fairly easily using<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar albums = Builder&lt;Album&gt;\r\n   .CreateListOfSize(10)\r\n   .All()\r\n      .With(a =&gt; a.Artist = Builder&lt;Artist&gt;.CreateNew()\r\n         .With(b =&gt; b.Band = Builder&lt;BandMember&gt;\r\n                    .CreateListOfSize(3)\r\n                       .Build().ToArray())\r\n\t.Build())\r\n   .Build();\r\n<\/pre>\n<p><strong>References<\/strong><\/p>\n<p>This is a great post on using <a href=\"http:\/\/www.jerriepelser.com\/blog\/creating-test-data-with-nbuilder-and-faker\" target=\"_blank\">NBuilder with Faker<\/a> which allows us to populate the objects with more realistic data than the default process. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was working on a small utility which generates XML based upon a given class (which is already XML serializable). I wanted to generate random data just so I could see how the end XML looked in case I needed to tweak the XSD. Source for the utility is available on AutoGenXml. I figured somebody [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[49,113,3,112],"tags":[],"class_list":["post-3571","post","type-post","status-publish","format-standard","hentry","category-net","category-autofixture","category-c","category-nbuilder"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/3571","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=3571"}],"version-history":[{"count":8,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/3571\/revisions"}],"predecessor-version":[{"id":3590,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/3571\/revisions\/3590"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=3571"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=3571"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=3571"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}