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 11/18/2019
2 FirstName2 LastName2 12/18/2019
3 FirstName3 LastName3 01/18/2020
4 FirstName4 LastName4 02/18/2020
5 FirstName5 LastName5 03/18/2020
6 FirstName6 LastName6 04/18/2020
7 FirstName7 LastName7 05/18/2020
8 FirstName8 LastName8 06/18/2020
9 FirstName9 LastName9 07/18/2020
10 FirstName10 LastName10 08/18/2020
11 FirstName11 LastName11 09/18/2020
12 FirstName12 LastName12 10/18/2020
13 FirstName13 LastName13 11/18/2020
14 FirstName14 LastName14 12/18/2020
15 FirstName15 LastName15 01/18/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 08/29/2019
101 Omar Hiemler 08/30/2019
102 Crystal Bartholomew 08/31/2019
103 Jorge Abdullah 09/01/2019
104 Summer Johannsen 09/02/2019
105 Naomi Ching 09/03/2019
106 Travis Pennington 09/04/2019
107 Zoey Hiemler 09/05/2019
108 Bartholomew 09/06/2019
109 Godfried Abdullah 09/07/2019
110 Mansoor Johannsen 09/08/2019
111 Zephyr Ching 09/09/2019
112 Fritz Pennington 09/10/2019
113 John Hiemler 09/11/2019
114 Patricia Bartholomew 09/12/2019
115 Michael Abdullah 09/13/2019
116 Susan Johannsen 09/14/2019
117 William Ching 09/15/2019
118 Mary Pennington 09/16/2019
119 Robert Hiemler 09/17/2019

© 2019 Will Crowther - SeedPacket