Hellnet.Database 1.0.9

There is a newer version of this package available.
See the version list below for details.
dotnet add package Hellnet.Database --version 1.0.9
                    
NuGet\Install-Package Hellnet.Database -Version 1.0.9
                    
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="Hellnet.Database" Version="1.0.9" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Hellnet.Database" Version="1.0.9" />
                    
Directory.Packages.props
<PackageReference Include="Hellnet.Database" />
                    
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 Hellnet.Database --version 1.0.9
                    
#r "nuget: Hellnet.Database, 1.0.9"
                    
#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 Hellnet.Database@1.0.9
                    
#: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=Hellnet.Database&version=1.0.9
                    
Install as a Cake Addin
#tool nuget:?package=Hellnet.Database&version=1.0.9
                    
Install as a Cake Tool

Hellnet Database

PostgreSQL-first database infrastructure library for .NET. Env-first, modular, cloud-native.

Env vars → HellnetDatabaseOptions → NpgsqlDataSource → IDatabaseExecutor / IRepository<T>

Quick Start

1. Install

dotnet add package Hellnet.Database

2. Set environment variables

export HELLNET_DATABASE_HOST=localhost
export HELLNET_DATABASE_PORT=5432
export HELLNET_DATABASE_NAME=mydb
export HELLNET_DATABASE_USERNAME=postgres
export HELLNET_DATABASE_PASSWORD=password

export HELLNET_DATABASE_POOL_MIN_SIZE=10
export HELLNET_DATABASE_POOL_MAX_SIZE=100
export HELLNET_DATABASE_COMMAND_TIMEOUT_SECONDS=30

3. Register in DI

using Hellnet.Database;

var builder = WebApplication.CreateBuilder(args);

// From environment variables (recommended)
builder.Services.AddHellnetDatabase();

// Or with explicit options
builder.Services.AddHellnetDatabase(new HellnetDatabaseOptions
{
    Host = "pg.internal",
    Database = "orders",
    Username = "app",
    Password = "secret",
});

4. Use

public class OrderService
{
    private readonly IDatabaseExecutor _db;
    private readonly IRepository<Order> _orders;

    public OrderService(IDatabaseExecutor db, IRepository<Order> orders)
    {
        _db = db;
        _orders = orders;
    }

    public async Task<IReadOnlyList<Order>> GetPendingAsync()
    {
        return await _db.QueryAsync<Order>(
            "SELECT * FROM orders WHERE status = 'pending'");
    }

    public async Task<Order?> GetByIdAsync(int id)
    {
        return await _orders.GetByIdAsync(id);
    }
}

Features

Connection Management

Uses NpgsqlDataSource for connection pooling, lifecycle, and diagnostics. Configured entirely via environment variables — no connection strings in code.

Env Default Description
HELLNET_DATABASE_HOST localhost PostgreSQL host
HELLNET_DATABASE_PORT 5432 PostgreSQL port
HELLNET_DATABASE_NAME (required) Database name
HELLNET_DATABASE_USERNAME (required) Database user
HELLNET_DATABASE_PASSWORD (required) Database password
HELLNET_DATABASE_POOL_MIN_SIZE 10 Minimum pool size
HELLNET_DATABASE_POOL_MAX_SIZE 100 Maximum pool size
HELLNET_DATABASE_COMMAND_TIMEOUT_SECONDS 30 Command timeout
HELLNET_DATABASE_RETRY_ENABLED true Enable retry on transient errors

Resilience (Polly)

Automatic retry with exponential backoff and permanent error discrimination. Does not retry:

SQL State Error
42601 Syntax error
23505 Unique violation
23503 Foreign key violation
42501 Permission denied
42P01 Undefined table
42703 Undefined column

Repository Pattern

public interface IRepository<T> where T : class
{
    Task<T?> GetByIdAsync<TId>(TId id, CancellationToken ct = default);
    Task<IReadOnlyList<T>> GetAllAsync(CancellationToken ct = default);
    Task<IReadOnlyList<T>> FindAsync(ISpecification<T> spec, CancellationToken ct = default);
    Task<PageResult<T>> PaginateAsync(ISpecification<T> spec, int page, int pageSize, CancellationToken ct = default);
    Task<int> CountAsync(ISpecification<T> spec, CancellationToken ct = default);
}

Transactions

await db.TransactionAsync(async tx =>
{
    await tx.ExecuteAsync("UPDATE accounts SET balance = balance - 100 WHERE id = 1");
    await tx.ExecuteAsync("UPDATE accounts SET balance = balance + 100 WHERE id = 2");
});

Typed Results

DatabaseResult<User> result = await DatabaseResult<User>.Success(user, duration);
if (result.IsSuccess) { /* use result.Data */ }

PageResult<Order> page = new()
{
    Items = orders,
    TotalCount = 100,
    Page = 1,
    PageSize = 20,
};

Architecture

Hellnet.Database
├── Abstractions/          ← Pure contracts (no Npgsql dependency)
│   ├── IDatabaseExecutor       Query, Execute, Scalar
│   ├── IDatabaseTransaction    Begin/Commit/Rollback
│   ├── IRepository<T>          Generic repository
│   ├── ISpecification<T>       Query specification
│   ├── DatabaseResult<T>       Typed result wrapper
│   └── PageResult<T>           Paginated result
├── Configuration/         ← Env-first (HELLNET_DATABASE_*)
│   ├── HellnetDatabaseOptions   Individual host/port/user/pass
│   └── DatabaseEnvBinder        Reads env vars, builds connection string
├── PostgreSql/            ← Npgsql implementation
│   ├── PostgresConnectionFactory  NpgsqlDataSource wrapper
│   ├── NpgsqlExecutor             Dapper-based executor
│   ├── NpgsqlTransaction          Transaction support
│   └── PostgresRepository<T>      Repository implementation
└── Resilience/            ← Polly retry with error discrimination
    └── DatabaseRetryPolicy

Package structure (future)

Hellnet.Database.Abstractions      → Contracts only (no Npgsql)
Hellnet.Database.PostgreSql        → PostgreSQL implementation
Hellnet.Database.Resilience        → Polly retry policies
Hellnet.Database.SqlServer         → (future)
Hellnet.Database.MySql             → (future)
Hellnet.Database.Testing           → (future)

Observability

This library provides infrastructure only. Observability (metrics, tracing, health checks, logging) is delegated to Hellnet.Observability.

To instrument database calls:

  • Use OpenTelemetry's Npgsql instrumentation to capture query duration, errors, and connection pool metrics
  • Hellnet.Observability handles OTel setup automatically

Configuration precedence

Environment Variables  ← highest priority
IConfiguration
HellnetDatabaseOptions
Default values         ← lowest priority

License

Apache 2.0 — see LICENSE.

Repo Purpose
hellnet-dep-kafka Kafka pub/sub library
hellnet-dep-observability OpenTelemetry + structured logging
hellnet-dep-cache Multi-layer cache (Memory → Valkey)
hellnet-dep-schema Schema registry management
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.

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.1.2 142 7/9/2026
1.0.9 124 7/9/2026