Faithlife.FakeData 2.1.5

Prefix Reserved
dotnet add package Faithlife.FakeData --version 2.1.5
                    
NuGet\Install-Package Faithlife.FakeData -Version 2.1.5
                    
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="Faithlife.FakeData" Version="2.1.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Faithlife.FakeData" Version="2.1.5" />
                    
Directory.Packages.props
<PackageReference Include="Faithlife.FakeData" />
                    
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 Faithlife.FakeData --version 2.1.5
                    
#r "nuget: Faithlife.FakeData, 2.1.5"
                    
#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 Faithlife.FakeData@2.1.5
                    
#: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=Faithlife.FakeData&version=2.1.5
                    
Install as a Cake Addin
#tool nuget:?package=Faithlife.FakeData&version=2.1.5
                    
Install as a Cake Tool

Faithlife.FakeData

Faithlife.FakeData is a fake in-memory relational database for prototypes and unit tests.

NuGet

Overview

Why use a fake in-memory database? In-memory databases can shorten the development cycle, especially during early development: they don't require disk resources, the database schema is easier to change, unit tests against an in-memory database run quickly, etc.

Why use a fake relational in-memory database? When it comes time to create an actual relational database implementation, the more the fake database looks like the actual database, the easier it will be.

While creating the actual database implementation, it's nice to already have passing unit tests. Once the actual database implementation is created, you can abandon the fake database, or you can keep both, and run unit tests against both the fake and the real databases. The justification of continuing to maintain the fake database is debatable, but you may find it useful for ongoing development, e.g. by writing new features against the fake database at first, and then following up with the actual implementation. Having two database implementations also encourages good abstractions, helping prevent database code from leaking into higher layers.

Faithlife.FakeData provides a trivial implementation of an in-memory relational database. There are no indexes, so every query is basically a "full table scan," which can be slow for large databases, especially for joins. This library is therefore great for prototypes and unit tests, but not appropriate for production.

The fake database does basic validation of column data, but there is room for improvement. For example, it does not currently support defining and enforcing foreign keys or unique columns.

Consult the source code for additional details.

Usage

Each table of your fake database should have a corresponding "record" class. (Try it!)

Each column of the table should have a corresponding read-write property on the record class. Use property types that correspond to data types supported by actual relational databases (integers, strings, etc.). Use a nullable type if the column should allow null.

Some attributes from System.ComponentModel.DataAnnotations are also supported:

  • KeyAttribute: Indicates the primary key. If the column type is int or long, it defaults to an "auto-increment" value when a row is added.
  • RequiredAttribute: Prevents the column from being set to null.
  • StringLengthAttribute: Prevents the column from being set to a string longer than the specified length.
  • RegularExpressionAttribute: Prevents the column from being set to a string that doesn't match the specified regular expression.
public sealed class UserRecord
{
    [Key]
    public long UserId { get; set; }

    [Required, StringLength(100)]
    public string Name { get; set; } = "";

    [RegularExpression(@"^[a-zA-Z0-9_]+$")]
    public string? Alias { get; set; }
}

public sealed class UserRoleRecord
{
    [Key]
    public long UserRoleId { get; set; }

    public long UserId { get; set; }

    public int Role { get; set; }
}

The database schema is represented by a class that derives from FakeDatabaseContext. It should have one read-only property of type FakeDatabaseTable<T> for each table in the database. Initialize each property in the constructor with a call to CreateTable<T>().

public sealed class UserDatabaseContext : FakeDatabaseContext
{
    public UserDatabaseContext()
    {
        Users = CreateTable<UserRecord>();
        UserRoles = CreateTable<UserRoleRecord>();
    }

    public FakeDatabaseTable<UserRecord> Users { get; }

    public FakeDatabaseTable<UserRoleRecord> UserRoles { get; }
}

To create an empty database, call FakeDatabase.Create<T>().

var database = FakeDatabase.Create<UserDatabaseContext>();

To add rows to a table, lock the database and call Add or AddRange on the table from the returned context.

using var context = database.Lock();
var alice = context.Users.Add(new UserRecord { Name = "Alice", Alias = "4l1c3" });
var bob = context.Users.Add(new UserRecord { Name = "Bob", Alias = "b0b" });
context.UserRoles.AddRange(new[]
{
    new UserRoleRecord { UserId = alice.UserId, Role = 1 },
    new UserRoleRecord { UserId = bob.UserId, Role = 2 },
    new UserRoleRecord { UserId = bob.UserId, Role = 1 },
});

To run a query, use LINQ to Objects on the tables, which implement IEnumerable<T>.

var aliases = context.Users.Select(x => x.Alias).Distinct();

LINQ query syntax is nice for inner joins.

var namesWithRoleTwo =
    from user in context.Users
    join role in context.UserRoles on user.UserId equals role.UserId
    where role.Role == 2
    select user.Name;

To update rows in a table, call UpdateWhere, which returns the number of rows affected.

context.Users.UpdateWhere(x => x.UserId == bob.UserId, x => x.Name = "Robert");

To remove rows from a table, call RemoveWhere, which also returns the number of rows affected.

context.Users.RemoveWhere(x => x.UserId == bob.UserId);

If you maintain both the fake database and the actual database, consider using the record classes with the actual database implementation. They can be useful when working with micro-ORMs like Dapper, e.g. by mapping a SELECT query into one or more record classes, or by using a record instance to supply parameters to an INSERT query.

Contributing

See Contributing for setup and contribution guidelines. For a history of notable changes, see the Release Notes.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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
2.1.5 102 5/27/2026
2.1.4 101 5/25/2026
2.1.3 100 5/25/2026
2.1.2 4,398 10/8/2022
2.1.1 1,125 2/19/2021
2.1.0 546 1/25/2021
2.0.0 779 11/13/2019
1.0.0 891 8/26/2019
0.1.2 956 5/21/2019
0.1.1 786 3/25/2019
0.1.0 750 3/25/2019