SimpleSeed

Created by Will Crowther

Overview

SimpleSeed is a simple and direct approach to data generation that is a part of SeedPacket. It provides a few extra tools to a Linq data generation workflow that can enhance quality of data simulation for mockups, unit testing, or when using "real" data is not appropriate. SimpleSeed provides two methods, Next and Randomize that allow you to easily pull data from an external Json or Xml data source and apply it to your data. Let's get started by looking at an example of how you may generate basic data using straight Linq, then we will move on to how SimpleSeed can be used to enhance that data and make it more realistic.

For information about the more full-featured .Seed() method, please go the Home page.

Generating Data With Linq

An easy and often overlooked method for generating data for mockups or testing is to use the Linq Range method paired with a Select method to succinctly create a list of objects. Range takes two parameters, an initial number, and the number of elements to create. We can use this capability to rapidly create a list of a particular type as shown in the code below and the results to the right.

While the generated data like this does the job for some situations, particularily for unit testing, it does not have a "real world" feel or the variety of real data. In our next section, we will look how SimpleSeed can be used to extend these techniques to take this simple kind of data generation to a more sophisticated and useful level.

Generic Linq Range with Select
// The starting integer and the number of items to create var userList = Enumerable.Range(1, 15) .Select(n => new User { UserId = n, FirstName = string.Format("FirstName" + n), LastName = string.Format("LastName" + n), Email = string.Format("FLastName" + n + "@test.com"), Created = DateTime.Now.AddMonths(n) }).ToList();
UserId FirstName LastName
Email Created
1 FirstName1 LastName1 FLastName1@test.com 11/21/2018
2 FirstName2 LastName2 FLastName2@test.com 12/21/2018
3 FirstName3 LastName3 FLastName3@test.com 01/21/2019
4 FirstName4 LastName4 FLastName4@test.com 02/21/2019
5 FirstName5 LastName5 FLastName5@test.com 03/21/2019
6 FirstName6 LastName6 FLastName6@test.com 04/21/2019
7 FirstName7 LastName7 FLastName7@test.com 05/21/2019
8 FirstName8 LastName8 FLastName8@test.com 06/21/2019
9 FirstName9 LastName9 FLastName9@test.com 07/21/2019
10 FirstName10 LastName10 FLastName10@test.com 08/21/2019
11 FirstName11 LastName11 FLastName11@test.com 09/21/2019
12 FirstName12 LastName12 FLastName12@test.com 10/21/2019
13 FirstName13 LastName13 FLastName13@test.com 11/21/2019
14 FirstName14 LastName14 FLastName14@test.com 12/21/2019
15 FirstName15 LastName15 FLastName15@test.com 01/21/2020

Generating Data With SimpleSeed

The code belows shows that we have added SeedPacket and are adding a SimpleSeed instance that points to the XML file "~/SourceFiles/seedsource.xml" in the root of the project. Using the .Next() method, data matching the string in the XML will be injected into the results so that we have a much more "real world" feel to the data!

The values returned are based on the ordinal that is passed into the loop, adjusted so that it falls into the range of the items in the XML. This means that the data will loop back when it runs out of new values. If the variety of output is too obviously repetitive, it is easy to add additional values to the seed data in the source.

The source that is used to generate the seed data can be either JSON or XML and can be either a string or be loaded from a file. SimpleSeed default to an Auto source type that will attempt to indentify which source type is being supplied and adjust to that type. A type can also be expicitly specified and will fail if the source is incorrect. If valid source can be found, an internal dictionary of limited resources is used.

SimpleSeed has another method called .Randomize that works like .Next but get the values brought back using a c# Random call instead of sequentially. As the call uses a default seed value for the Random, the sequence of returned values will generally be the same unless you initialize a new seed value for the Random or change the size of elements that you are drawing from.

Be sure to import the SeedPacket namespace.
// Gets data from the referenced .xml file var ss = new SimpleSeed("~/SourceFiles/seedsource.xml"); var userList = Enumerable.Range(100, 20) .Select(n => new User { UserId = n, FirstName = ss.Next("FirstName", n), LastName = ss.Next("LastName", n), Email = ss.Next("FirstName", n).FirstOrDefault() + ss.Next("LastName", n) + "@" + ss.Next("CompanyName", n) + ss.Next("DomainExtension", n), Created = DateTime.Now.AddDays(n-150) }).ToList();
UserId FirstName LastName
Email Created
100 Theresa Pennington TPennington@Acme.com 09/01/2018
101 Omar Hiemler OHiemler@Rand.com 09/02/2018
102 Crystal Bartholomew CBartholomew@Sears.net 09/03/2018
103 Jorge Abdullah JAbdullah@Acme.com 09/04/2018
104 Summer Johannsen SJohannsen@Rand.com 09/05/2018
105 Naomi Ching NChing@Sears.net 09/06/2018
106 Travis Pennington TPennington@Acme.com 09/07/2018
107 Zoey Hiemler ZHiemler@Rand.com 09/08/2018
108 Bartholomew Bartholomew@Sears.net 09/09/2018
109 Godfried Abdullah GAbdullah@Acme.com 09/10/2018
110 Mansoor Johannsen MJohannsen@Rand.com 09/11/2018
111 Zephyr Ching ZChing@Sears.net 09/12/2018
112 Fritz Pennington FPennington@Acme.com 09/13/2018
113 John Hiemler JHiemler@Rand.com 09/14/2018
114 Patricia Bartholomew PBartholomew@Sears.net 09/15/2018
115 Michael Abdullah MAbdullah@Acme.com 09/16/2018
116 Susan Johannsen SJohannsen@Rand.com 09/17/2018
117 William Ching WChing@Sears.net 09/18/2018
118 Mary Pennington MPennington@Acme.com 09/19/2018
119 Robert Hiemler RHiemler@Rand.com 09/20/2018

© 2018 Will Crowther - SeedPacket