DataMaker 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package DataMaker --version 1.0.0
                    
NuGet\Install-Package DataMaker -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DataMaker" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DataMaker" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="DataMaker" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DataMaker --version 1.0.0
                    
#r "nuget: DataMaker, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package DataMaker@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DataMaker&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=DataMaker&version=1.0.0
                    
Install as a Cake Tool

DataMaker

<img src="./icon.png" alt="DataMaker Logo" width="100" />

A .NET library for quickly generating test data entities from existing data sources.

.NET 8.0 License: MIT

Overview

DataMaker simplifies test data generation by mapping your existing database tables (or other data sources) to strongly-typed C# entities. Instead of writing complex test data setup code, define your mappings once and generate as many test instances as you need.

Key Features

  • Strongly-typed mappings using lambda expressions
  • Foreign key lookups - automatically resolve relationships between tables
  • Multiple selection strategies - sequential or random data selection
  • Provider pattern - easily extend to support different data sources (SQL Server, CSV, etc.)
  • Fluent API - clean, readable configuration
  • Type-safe - compile-time validation of your mappings

Installation

NuGet Package

dotnet add package DataMaker

Or via Package Manager Console:

Install-Package DataMaker

Build from Source

git clone https://github.com/yourusername/DataMaker.git
cd DataMaker
dotnet build

Quick Start

Basic Usage

using DataMaker;

// 1. Create a DataMaker instance
var maker = new DataMaker();

// 2. Add a data provider (SQL Server, CSV, etc.)
maker.AddProvider(new SqlServerDataProvider("YourConnectionString"));

// 3. Configure mappings for your entity
maker.AddDataMap<User>("Users")  // Primary table name
    .WithColumn(u => u.Name, "Name")
    .WithColumn(u => u.Email, "Email");

// 4. Generate test data
var users = maker.Generate<User>(10);  // Generate 10 users

foreach (var user in users)
{
    Console.WriteLine($"{user.Name} - {user.Email}");
}

Entity Definition

public class User
{
    public string? Name { get; set; }
    public string? Email { get; set; }
    public string? City { get; set; }
}

Core Concepts

1. Data Providers

Data providers implement IDataProvider and retrieve data from a source. DataMaker currently includes:

  • SqlServerDataProvider - Retrieves data from SQL Server databases
var provider = new SqlServerDataProvider(
    "Server=.;Database=TestDb;Integrated Security=True;"
);
maker.AddProvider(provider);

2. Primary Tables

Each entity maps to a primary table. When generating data, DataMaker selects rows from this table based on your chosen strategy.

maker.AddDataMap<Product>("Products")  // "Products" is the primary table

3. Selection Strategies

Control how rows are selected from the primary table:

// Sequential - rows 0, 1, 2, 3... (wraps around if count > rows)
var sequential = maker.Generate<User>(10, SelectionStrategy.Sequential);

// Random - randomly selected rows
var random = maker.Generate<User>(10, SelectionStrategy.Random);

Mapping Methods

WithColumn - Simple Column Mapping

Maps a property directly to a column in the primary table.

maker.AddDataMap<User>("Users")
    .WithColumn(u => u.Name, "FullName")
    .WithColumn(u => u.Email, "EmailAddress");

WithLookup - Foreign Key Relationships

Automatically resolves foreign key relationships to related tables.

maker.AddDataMap<User>("Users")
    .WithColumn(u => u.Name, "Name")
    .WithLookup(
        u => u.City,           // Target property
        "Addresses",           // Related table
        "AddressId",           // Foreign key in primary table (Users)
        "Id",                  // Primary key in related table (Addresses)
        "City"                 // Column to retrieve from related table
    );

How it works:

  1. Selects a row from the Users table
  2. Reads the AddressId value from that row
  3. Finds the matching row in Addresses where Id = AddressId
  4. Returns the City value from the matched address row

WithTableMap - Custom Mappings

For complex scenarios, use custom mapping functions with access to the row and provider.

maker.AddDataMap<User>("Users")
    .WithColumn(u => u.FirstName, "FirstName")
    .WithColumn(u => u.LastName, "LastName")
    .WithTableMap(
        u => u.FullName,
        (row, provider) => $"{row["FirstName"]} {row["LastName"]}"
    );

Advanced Examples

Complex Entity with Multiple Lookups

public class Order
{
    public string? OrderNumber { get; set; }
    public string? CustomerName { get; set; }
    public string? ProductName { get; set; }
    public decimal? Price { get; set; }
}

maker.AddDataMap<Order>("Orders")
    .WithColumn(o => o.OrderNumber, "OrderNum")
    .WithLookup(o => o.CustomerName, "Customers", "CustomerId", "Id", "Name")
    .WithLookup(o => o.ProductName, "Products", "ProductId", "Id", "Name")
    .WithLookup(o => o.Price, "Products", "ProductId", "Id", "Price");

Combining Multiple Tables

maker.AddDataMap<Employee>("Employees")
    .WithColumn(e => e.EmployeeId, "Id")
    .WithColumn(e => e.FirstName, "FirstName")
    .WithColumn(e => e.LastName, "LastName")
    .WithLookup(e => e.DepartmentName, "Departments", "DeptId", "Id", "Name")
    .WithLookup(e => e.ManagerName, "Employees", "ManagerId", "Id", "FirstName")
    .WithLookup(e => e.City, "Addresses", "AddressId", "Id", "City")
    .WithLookup(e => e.State, "Addresses", "AddressId", "Id", "State");

var employees = maker.Generate<Employee>(50, SelectionStrategy.Random);

Custom Data Transformation

maker.AddDataMap<Product>("Products")
    .WithColumn(p => p.Name, "ProductName")
    .WithTableMap(
        p => p.DisplayName,
        (row, provider) =>
        {
            var name = row["ProductName"].ToString();
            var category = row["Category"].ToString();
            return $"{category} - {name}";
        }
    );

API Reference

DataMaker Class

Method Description
AddProvider(IDataProvider) Adds a data provider for retrieving data
AddDataMap<T>(string tableName) Creates a mapping configuration for type T with specified primary table
Generate<T>(int count, SelectionStrategy) Generates specified number of entities using the configured mappings

DataMap<T> Class

Method Description
WithColumn(property, columnName) Maps property to column in primary table
WithLookup(property, table, fk, pk, column) Maps property using foreign key lookup
WithTableMap(property, func) Maps property using custom function

SelectionStrategy Enum

Value Description
Sequential Selects rows in order (0, 1, 2...), wraps around if needed
Random Randomly selects rows from available data

Data Providers

SQL Server Provider

var provider = new SqlServerDataProvider(connectionString);

Features:

  • Lazy loading and caching of table data
  • SQL injection protection via table name validation
  • Support for standard SQL Server table naming conventions

Security: Table names are validated using a whitelist pattern (alphanumeric, underscore, period, brackets only).

Creating Custom Providers

Implement IDataProvider to support other data sources:

public class CsvDataProvider : IDataProvider
{
    public IDataEntity this[string entityName]
    {
        get
        {
            // Load CSV file and return as IDataEntity
        }
    }
}

Best Practices

  1. Reuse DataMaker instances - Configure once, generate many times
  2. Use WithColumn for simple mappings - More performant than custom functions
  3. Cache related table data - Providers cache table data automatically
  4. Validate your mappings - Run tests to ensure foreign keys resolve correctly
  5. Choose appropriate selection strategy - Use Sequential for deterministic tests, Random for broader coverage

Performance Considerations

  • Table caching: Each table is loaded once and cached by the provider
  • Foreign key lookups: Performed via LINQ queries on cached data
  • Large datasets: Consider using views or filtered queries in your provider

Troubleshooting

"No data provider has been added"

Call AddProvider() before calling Generate().

"Primary table 'X' has no rows"

Ensure your database table contains data.

Foreign key lookup returns null

  • Verify the foreign key column name matches your database
  • Ensure related table has matching records
  • Check that key values match (types and values)

Type conversion errors

DataMaker attempts automatic type conversion. For complex types, use WithTableMap with custom conversion logic.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Roadmap

  • CSV data provider implementation
  • JSON data provider
  • Support for multiple data providers in single DataMaker instance
  • Performance optimizations for large datasets
  • Fluent assertion extensions for testing
  • Table join support
  • Data transformation pipelines

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

For issues, questions, or suggestions:

  • Open an issue on GitHub
  • Check existing issues for solutions
  • Review the examples in the test project

Acknowledgments

Built with ❤️ for the .NET testing community.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.4.1 202 1/27/2026
1.4.0 64 1/26/2026
1.3.2 90 1/19/2026
1.3.1 61 1/15/2026
1.3.0 60 1/14/2026
1.2.0 63 1/14/2026
1.1.0 71 1/9/2026
1.0.0 63 1/8/2026

v1.0.0 - Initial Release
     - SQL Server data provider
     - Strongly-typed property mappings with lambda expressions
     - Foreign key lookup support (WithLookup)
     - Sequential and Random selection strategies
     - Custom mapping functions (WithTableMap)
     - SQL injection protection
     - Comprehensive XML documentation