version 2 in C#
Created by Will Crowther
The source for this example is in the view and is similar to the earlier Home "Primitive Lists" example except that it uses custom Sport data from a custom Xml (or JSON) file. "XmlSeedSourcePlus.xml" is based on the default source xml, but with the addition of some extra data such as "Descriptions" and "Sports".
For either complex types or primitive types like this, string fields will generate random data matching the property name (ie: "Sport", not the plural) that is being populated by default. If the property name does not exactly match the data from the source file, a custom Rule can be created that can explicitly name the field.
The default XML datasource file to download (and modify).
The default JSON datasource file to download (and modify).
If you are going to keep the existing default rules such as "StateName", "FirstName", etc. in your Rules collection, it is generally better to only add to or modify the existing data, rather than deleting it. If you do delete the source data for an existing Rule, it should not throw an error, but will be very generic data like 'StateName1' or 'FirstName1'.
Customizing the Generator
This example is more advanced and has several things going on besides just using the "XmlSeedSourcePlus.xml" source file. It has 5 custom rules, which show off a few interesting things.
One is that it uses the custom Description data. If you look closely at the Sports example above, you will notice that "Rugby" and "Bicycling" are repeated twice. This is because the data is selected from the source "Randomly" using a method Funcs.GetElementRandom() If we want this list to be sequential, we can use a similar method called Funcs.GetElementNext() as we are doing for the "Description" Rule. This will take the first item from the source, then go to next, etc. looping back to front when it gets to the end.
You might notice that the xml source first element for Description is "It was kinda big" but the first item displayed on the page is "Bumpy". This would not seem to make sense until you realize that the determining number for the ordinal is the current RowNumber, which is 100 and "Bumpy" is the hundredth item in the list. Funcs.GetElementNext() does have an optional OffSet parameter so that you can shift the list without having to depend on the RowNumber.
Another interesting feature is that we are generating a list of Seeded Items that are then stored in the generator.Cache. This is a place to put generated data that can then be accessed by a Rule. The Products Rule is doing just that and is populating a sequential list from the generator.Cache.Items. Funcs.GetCacheItemsNext works similar to Funcs.GetElementNext() except it works at the object level and returns a sequential list of items. Note that the list of Items is always the same as it is pulled from the cache and will alway be unique it the number requested is less or equal to the number number in the source list. More on this in the 25,000 Rows example.
Finally, we are generating a "SelectedProduct" in the Example object that is populated by a Random number from 0 to 10 and this is what is being used to populate the selected product in the Products dropdown.
|100||Next Co||Oakleaf||GA||Collins||$139.55M||Square, man||54f931a0-3457-8ab6-6224-8aa04175913e|
|101||Primo Co||Pinetown||VT||John Smith||$550.67M||Smelly but good||e9ce673c-02ad-456e-0107-df6eff29f370|
|104||Acme Co||Kingston||AL||Susan Jones||$346.52M||Who knows||d2578136-63d9-99b5-cad8-25cab18dd77a|
|105||More Co||Oakleaf||AR||William Brown||$150.93M||Bumpy||0ecd1b54-ca2f-ef87-b9da-c592c03e5848|
|106||City Inc||WestLake||PA||Mary Davis||$48.37M||Angular||b23c0acb-9db5-f62a-3a95-245ae5ec642e|
|108||Umbrella Corp||Franklin||ND||Sarah Wilson||$703.47M||Angular||1aec04dd-1979-de90-7fd2-263933509939|
|110||Wonka Industries||Eastdale||OH||Ann Taylor||$65.64M||Sunny and bright||4bce7ccb-9b36-82dc-e0fa-5009bb77d5f6|
|112||Stark Inc||York||HI||Theresa Thomas||$274.48M||Light purple||2b379c60-906a-7467-7cdf-5e7484dc4cfa|
|113||CyberGlobe Corp||Peachtree||VT||Ethan Jackson||$684.89M||Yellow, definitely yellow||ab9205c9-665c-4f83-04a0-d25e4cd6f203|
|114||Nakatomi Co||Oakleaf||NY||Crystal White||$167.43M||Hairy and green||ed8182f7-8c8a-ab69-0deb-848efc5aaef7|
|115||Stark Co||Gotham||AZ||Phillip Harris||$350.10M||Midnight blue||7186dd76-935d-dcb1-65cc-1465cebade03|