Activout.DatabaseClient 5.0.0

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

Activout Database Client

Create a database client only by defining the C# interface you want. Uses Dapper for object mapping.

Create a Database Access Object (DAO) defining the C# interface you want and writing the SQL query. Shamelessly inspired by Jdbi Declarative API.

API documentation

Rationale

The Activout Database Client provides a type-safe approach to make SQL requests to the database.

For the actual object mapping, Dapper is used but another implementation can be configured.

Example

public interface IUserDaoAsync
{
    [SqlUpdate("CREATE TABLE user (id INTEGER PRIMARY KEY, name VARCHAR)")]
    Task CreateTable();

    [SqlUpdate("INSERT INTO user(id, name) VALUES (@id, @name)")]
    Task InsertNamed([Bind("id")] int id, [Bind("name")] string name);

    [SqlUpdate("INSERT INTO user(id, name) VALUES (@id, @name)")]
    Task InsertObject([BindProperties] User user);

    [SqlUpdate("INSERT INTO user(id, name) VALUES (@user_id, @user_name)")]
    Task InsertObjectFull([BindProperties] User user);

    [SqlQuery("SELECT * FROM user ORDER BY name")]
    Task<IEnumerable<User>> ListUsers();

    [SqlQuery("SELECT * FROM user WHERE id = @id")]
    Task<User> GetUserById(int id);
}


_userDao = new DatabaseClientBuilder()
    .With(new DapperGateway(sqliteConnection))
    .Build<IUserDaoAsync>();

await _userDao.CreateTable();
await _userDao.InsertNamed(42, null);
var user = await _userDao.GetUserById(42);

Full example code in DatabaseClientAsyncTest.cs.

Transactions

Extend your DAO interface with IWithTransactions to begin transactions, and add an IDbTransaction parameter to the methods that should run in one. Passing null runs the statement without a transaction.

public interface IUserDao : IWithTransactions
{
    [SqlUpdate("INSERT INTO user(id, name) VALUES (@id, @name)")]
    Task InsertNamed(int id, string name, IDbTransaction? transaction);
}

using var transaction = userDao.BeginTransaction();
await userDao.InsertNamed(42, "foobar", transaction);
transaction.Commit();

Some providers, such as Microsoft.Data.Sqlite, require every statement to pass the pending transaction.

Example with dependency injection


// Connection string example for MySQL:
// "Server=example.com;Database=example;User=example;Password=example;AutoEnlist=True;Pooling=True;ConnectionReset=True;CharSet=utf8mb4;"
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddScoped<IDbConnection>(_ => new MySqlConnection(connectionString));

builder.Services.AddScoped<DatabaseClientBuilder>();
builder.Services.AddScoped<IDatabaseClient>(sp => sp
    .GetRequiredService<DatabaseClientBuilder>()
    .With(new DapperGateway(sp.GetRequiredService<IDbConnection>()))
    .Build<IDatabaseClient>());

builder.Services.AddTransient<SomeService>();

// ...

public class SomeService(IDatabaseClient databaseClient)
{
    // ...
}


Shared connection and cross-DAO transactions

The Activout.DatabaseClient.Dapper.Extensions.DependencyInjection package reduces the boilerplate:

builder.Services.AddScoped<IDbConnection>(_ => new NpgsqlConnection(connectionString));
builder.Services.AddDapperDatabaseClient<IOrdersDao>();
builder.Services.AddDapperDatabaseClient<IBooksDao>();

A transaction can be passed from one DAO to another only if all of them use the same connection, so register the IDbConnection as scoped. The container owns and disposes the connection, and DapperGateway opens it if closed. For other gateways, use AddDatabaseClient<TDao>(gatewayFactory) from Activout.DatabaseClient.Extensions.DependencyInjection.

Collaborate

This project is still under development - participation welcome!

  • Activout.RestClient - Create a REST(ish) API client only by defining the C# interface you want.

About Activout

Activout AB is a software company in Ronneby, Sweden.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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.
  • net10.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Activout.DatabaseClient:

Package Downloads
Activout.DatabaseClient.Dapper

Dapper Backend for Activout.DatabaseClient.

Activout.DatabaseClient.Extensions.DependencyInjection

Microsoft.Extensions.DependencyInjection extensions for Activout.DatabaseClient.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
5.0.0 291 10/23/2026
5.0.0-rc5 121 10/16/2026
5.0.0-rc4 180 10/7/2026
5.0.0-rc3 127 10/2/2026
5.0.0-rc2 104 9/29/2026
5.0.0-rc1 448 3/4/2025
4.0.0 338 1/11/2025
3.0.0-beta 594 10/17/2020
1.0.1 1,950 12/2/2018
1.0.0 1,604 12/2/2018