Data Generator API Sample

From the beginning, our main goal for the design of the API was simplicity. A third party tool used to save development time should offer as small of a learning curve as possible.  We worked really hard to build an expressive API that reads well through intellisense in Visual Studio.

As listed below is data generation summed up in four steps:

  1. Create a target for the desired database vendor
  2. Choose tables and options for each table
  3. Apply generators per column
  4. Generate test data

Walkthrough Generating Data into VistaDB 4

1. Creating a generation target

TargetFactory factory = TargetFactories.GetFactory("VistaDB");
Target target = factory.GetTarget(@"Data Source = '|DataDirectory|SampleDB.vdb4';");

Above we have created a new generation target and chose the VistaDB implementation. This target object will manage all database schema information and the Generators for our project. The target object accepts a connection string parameter used for database connection.

2. Choose tables to be generated, amount of rows and the option to truncate before insertion.

// I want this table to be generated
target.Tables.Where(t => t.TableName == "Employee").First().GenerateTable = true;

// I want to truncate this table before insertion (start with an empty table)
target.Tables.Where(t => t.TableName == "Employee").First().TruncateOnInsert = true;

// I want 1000 rows generated into this table
target.Tables.Where(t => t.TableName == "Employee").First().RowCount = 1000;

Using a LINQ statement we can query the Tables collection of our Target and set our “Employees” table to be generated, truncate and we want 1000 rows.

3. Apply the desired value Generators to our columns.

foreach (TargetColumn c in target.Tables.Where(t => t.TableName == "Employee").First().Columns)
{
    switch (c.ColumnName)
    {
        case "EmployeeID":
            {
                //EmployeeID is an autoincremented int, a dummygenerator
                //will insure no data goes into this column.
                c.Generator = new DummyGenerator();
                Console.WriteLine("Setting dummy generator");
            }
            break;
}

Using another LINQ statement we can query the Columns collection of our “Employees” table. Knowing that the column “FirstName” is an NVarChar meant to store the first names of our employees we can attach a new FirstNameGenerator to the Generator field of that column. The FirstNameGenerator accepts an integer parameter for the seed value and a Boolean for the UniqueValue field. The Seed value used by all generators ensures any sequence of data generated can be repeated later. The UniqueValue field, also used by all generators is used to determine if the value generated must be unique among all values generated for its column.

4. Generate the test data.

if (!target.Generate(out errors))

Calling Generate on the target will insert the desired rows of any Table object set to be generated and return a Boolean on success or failure.

Summary

Generating repeatable or random data into a VistaDB, SQL Server, SQL CE or mySQL database works the same way.  You could in fact use the exact same code across all of them with only changing the target creation in step 1.

Data Generation Home