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

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 12/30/2017
101 Omar Hiemler 12/31/2017
102 Crystal Bartholomew 01/01/2018
103 Jorge Abdullah 01/02/2018
104 Summer Johannsen 01/03/2018
105 Naomi Ching 01/04/2018
106 Travis Pennington 01/05/2018
107 Zoey Hiemler 01/06/2018
108 Bartholomew 01/07/2018
109 Godfried Abdullah 01/08/2018
110 Mansoor Johannsen 01/09/2018
111 Zephyr Ching 01/10/2018
112 Fritz Pennington 01/11/2018
113 John Hiemler 01/12/2018
114 Patricia Bartholomew 01/13/2018
115 Michael Abdullah 01/14/2018
116 Susan Johannsen 01/15/2018
117 William Ching 01/16/2018
118 Mary Pennington 01/17/2018
119 Robert Hiemler 01/18/2018

© 2018 Will Crowther - SeedPacket