Created by Will Crowther


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 + ""), Created = DateTime.Now.AddMonths(n) }).ToList();
UserId FirstName LastName
Email Created
1 FirstName1 LastName1 02/16/2019
2 FirstName2 LastName2 03/16/2019
3 FirstName3 LastName3 04/16/2019
4 FirstName4 LastName4 05/16/2019
5 FirstName5 LastName5 06/16/2019
6 FirstName6 LastName6 07/16/2019
7 FirstName7 LastName7 08/16/2019
8 FirstName8 LastName8 09/16/2019
9 FirstName9 LastName9 10/16/2019
10 FirstName10 LastName10 11/16/2019
11 FirstName11 LastName11 12/16/2019
12 FirstName12 LastName12 01/16/2020
13 FirstName13 LastName13 02/16/2020
14 FirstName14 LastName14 03/16/2020
15 FirstName15 LastName15 04/16/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 11/27/2018
101 Omar Hiemler 11/28/2018
102 Crystal Bartholomew 11/29/2018
103 Jorge Abdullah 11/30/2018
104 Summer Johannsen 12/01/2018
105 Naomi Ching 12/02/2018
106 Travis Pennington 12/03/2018
107 Zoey Hiemler 12/04/2018
108 Bartholomew 12/05/2018
109 Godfried Abdullah 12/06/2018
110 Mansoor Johannsen 12/07/2018
111 Zephyr Ching 12/08/2018
112 Fritz Pennington 12/09/2018
113 John Hiemler 12/10/2018
114 Patricia Bartholomew 12/11/2018
115 Michael Abdullah 12/12/2018
116 Susan Johannsen 12/13/2018
117 William Ching 12/14/2018
118 Mary Pennington 12/15/2018
119 Robert Hiemler 12/16/2018

© 2019 Will Crowther - SeedPacket