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 04/30/2020
2 FirstName2 LastName2 05/31/2020
3 FirstName3 LastName3 06/30/2020
4 FirstName4 LastName4 07/31/2020
5 FirstName5 LastName5 08/31/2020
6 FirstName6 LastName6 09/30/2020
7 FirstName7 LastName7 10/31/2020
8 FirstName8 LastName8 11/30/2020
9 FirstName9 LastName9 12/31/2020
10 FirstName10 LastName10 01/31/2021
11 FirstName11 LastName11 02/28/2021
12 FirstName12 LastName12 03/31/2021
13 FirstName13 LastName13 04/30/2021
14 FirstName14 LastName14 05/31/2021
15 FirstName15 LastName15 06/30/2021

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 02/10/2020
101 Omar Hiemler 02/11/2020
102 Crystal Bartholomew 02/12/2020
103 Jorge Abdullah 02/13/2020
104 Summer Johannsen 02/14/2020
105 Naomi Ching 02/15/2020
106 Travis Pennington 02/16/2020
107 Zoey Hiemler 02/17/2020
108 Bartholomew 02/18/2020
109 Godfried Abdullah 02/19/2020
110 Mansoor Johannsen 02/20/2020
111 Zephyr Ching 02/21/2020
112 Fritz Pennington 02/22/2020
113 John Hiemler 02/23/2020
114 Patricia Bartholomew 02/24/2020
115 Michael Abdullah 02/25/2020
116 Susan Johannsen 02/26/2020
117 William Ching 02/27/2020
118 Mary Pennington 02/28/2020
119 Robert Hiemler 02/29/2020

© 2020 Will Crowther - SeedPacket