Muonroi.Data.Dapper 1.0.0-alpha.16

This is a prerelease version of Muonroi.Data.Dapper.
dotnet add package Muonroi.Data.Dapper --version 1.0.0-alpha.16
                    
NuGet\Install-Package Muonroi.Data.Dapper -Version 1.0.0-alpha.16
                    
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="Muonroi.Data.Dapper" Version="1.0.0-alpha.16" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Muonroi.Data.Dapper" Version="1.0.0-alpha.16" />
                    
Directory.Packages.props
<PackageReference Include="Muonroi.Data.Dapper" />
                    
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 Muonroi.Data.Dapper --version 1.0.0-alpha.16
                    
#r "nuget: Muonroi.Data.Dapper, 1.0.0-alpha.16"
                    
#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 Muonroi.Data.Dapper@1.0.0-alpha.16
                    
#: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=Muonroi.Data.Dapper&version=1.0.0-alpha.16&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Muonroi.Data.Dapper&version=1.0.0-alpha.16&prerelease
                    
Install as a Cake Tool

Muonroi.Data.Dapper

Dapper integration for Muonroi: lightweight read-side repository, multi-tenant query filtering, and connection factory.

NuGet License: Apache 2.0

Muonroi.Data.Dapper plugs Dapper into the Muonroi multi-tenant stack. It adds a connection-string provider that reads from IConfiguration, a command builder (MDapperCommand) that bridges Muonroi's query model to Dapper's CommandDefinition, custom SqlMapper type handlers for Protobuf timestamps and trimmed strings, and an optional Row-Level Security (RLS) override that transparently applies the correct session context before every query or execute call — with no changes to your existing Dapper usage.

Installation

dotnet add package Muonroi.Data.Dapper --prerelease

Quick Start

Register the type handlers early in startup, then optionally enable RLS after your provider-specific Dapper registration:

using Muonroi.Data.Dapper.Dapper.Handlers;
using Muonroi.Data.Dapper.Rls;

// 1. Register Dapper type handlers globally (process-wide, no DB connection required).
MSqlMapperTypeExtensions.RegisterDapperHandlers();

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

// 2. Add your provider-specific IDapper (e.g. AddDapperForPostgreSQL from Dapper.Extensions.NetCore).
// ...

// 3. Optionally layer on RLS. This is a no-op when MultiTenantConfigs.EnableRowLevelSecurity
//    is false (the default), so it is safe to call unconditionally.
builder.Services.AddMuonroiDapperRls();

WebApplication app = builder.Build();
app.MapControllers();
app.Run();

When RLS is enabled, every IDapper call automatically runs SET app.current_tenant_id = @tid (PostgreSQL) or EXEC sp_set_session_context ... (MSSQL) before executing — no changes to your query code.

For cross-tenant admin operations, wrap with a bypass scope:

using (DapperRlsBypass.Enter())
{
    // Dapper queries here run cross-tenant (SET ROLE app_rls_bypass on PostgreSQL).
}

Features

  • MConnectionStringProvider — reads <name>:ConnectionString from IConfiguration and implements IConnectionStringProvider for Dapper.Extensions.NetCore.
  • MDapperCommand — command builder (CommandText, Parameters, Transaction, CommandType, CommandFlags) with a Build(CancellationToken) method that produces a CommandDefinition.
  • MSqlMapperTypeExtensions.RegisterDapperHandlers() — registers MProtobufTimestampHandler (Google Protobuf Timestamp) and MTrimStringHandler (auto-trim strings) as global SqlMapper type handlers.
  • AddMuonroiDapperRls() — replaces the live IDapper with TenantRlsDapper<TConn> when MultiTenantConfigs.EnableRowLevelSecurity is true; zero-impact early return otherwise (CFG-01).
  • TenantRlsDapper<TConn> — full override of all 110+ Dapper.Extensions.NetCore overloads; re-applies session context on every call (set-per-open, safe for pooled connections).
  • Provider support: PostgreSQL (end-to-end), MSSQL (end-to-end); MySQL deferred.
  • DapperRlsBypass.Enter()AsyncLocal-backed cross-tenant bypass scope for admin operations; every bypassed connection open is audit-logged.
  • IRlsGuaranteeProvider — singleton introspection of the active RLS enforcement strength (Native for PostgreSQL/MSSQL).
  • Startup verifier (RlsStartupVerifier) — hosted service that asserts required RLS DDL objects exist on startup; configurable via VerifyRlsObjectsOnStartup.
  • Strict mode (StrictMode = true) — throws MissingTenantContextException at query time when no tenant context and no bypass scope are active.

Configuration

appsettings.json

{
  "MultiTenantConfigs": {
    "EnableRowLevelSecurity": true,
    "DapperRls": {
      "Provider": "PostgreSql",
      "BypassRoleName": "app_rls_bypass",
      "StrictMode": false,
      "VerifyRlsObjectsOnStartup": true
    }
  }
}

DI registration

// Optional delegate overrides config-file values.
builder.Services.AddMuonroiDapperRls(opts =>
{
    opts.Provider = DapperRlsProvider.MsSql;
    opts.StrictMode = true;
});

AddMuonroiDapperRls must be called after your provider-specific AddDapperForXxx registration so that the services.Replace wins as the last descriptor (last-wins semantics).

DapperRlsOptions reference

Property Default Description
Provider PostgreSql PostgreSql, MsSql, or MySql (MySql deferred, throws at registration)
BypassRoleName "app_rls_bypass" PostgreSQL role granted BYPASSRLS; used by DapperRlsBypass.Enter()
StrictMode false Throw MissingTenantContextException when tenant id is absent and no bypass is active
VerifyRlsObjectsOnStartup true Fail fast at startup if required RLS DDL objects are missing

API Reference

Type Purpose
MConnectionStringProvider Implements IConnectionStringProvider; reads <name>:ConnectionString from IConfiguration
MDapperCommand Command builder; produces a CommandDefinition via Build(CancellationToken)
MSqlMapperTypeExtensions RegisterDapperHandlers() — installs Protobuf + trim-string SqlMapper type handlers
MProtobufTimestampHandler SqlMapper.TypeHandler<Timestamp> for Google Protobuf Timestamp columns
MTrimStringHandler SqlMapper.TypeHandler<string> that auto-trims string values
DapperRlsServiceCollectionExtensions AddMuonroiDapperRls(Action<DapperRlsOptions>?) — the primary DI registration extension
DapperRlsOptions Options bound from MultiTenantConfigs:DapperRls; see table above
DapperRlsProvider Enum: PostgreSql, MsSql, MySql
TenantRlsDapper<TConn> BaseDapper<TConn> subclass; enforces tenant context before every query/execute
ITenantSessionContextSetter Contract for provider-specific session-context setters (Apply / ApplyAsync)
PostgreSqlTenantSessionContextSetter Issues SET app.current_tenant_id = @tid (or SET ROLE <bypass>)
MsSqlTenantSessionContextSetter Issues EXEC sp_set_session_context @key=N'TenantId', @value=@tid
DapperRlsBypass Static Enter() + IsActive; AsyncLocal-backed cross-tenant bypass scope
IBypassScope Disposable scope returned by DapperRlsBypass.Enter()
IRlsGuaranteeProvider Singleton; exposes GuaranteeLevel (resolved from DapperRlsProvider at registration)
MissingTenantContextException Thrown in strict mode when tenant id is absent and no bypass is active
RlsObjectsMissingException Thrown by startup verifier when required RLS DDL objects are not found

Samples

  • Quickstart.Data.Dapper — demonstrates MDapperCommand, RegisterDapperHandlers, DapperRlsBypass, and the RLS provider/guarantee model without a live database

Compatibility

  • Target framework: net8.0
  • License: Apache-2.0 (OSS)

License

Apache-2.0. See LICENSE-APACHE.

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 (1)

Showing the top 1 NuGet packages that depend on Muonroi.Data.Dapper:

Package Downloads
Muonroi.BuildingBlock.All

Metapackage for Muonroi Building Block - Includes all sub-packages for a complete infrastructure setup.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0-alpha.16 0 6/22/2026
1.0.0-alpha.15 78 5/31/2026
1.0.0-alpha.14 76 5/15/2026
1.0.0-alpha.13 71 5/2/2026
1.0.0-alpha.12 75 4/2/2026
1.0.0-alpha.11 128 4/2/2026
1.0.0-alpha.9 69 3/30/2026
1.0.0-alpha.8 157 3/28/2026
1.0.0-alpha.7 78 3/27/2026
1.0.0-alpha.5 62 3/27/2026
1.0.0-alpha.4 65 3/27/2026
1.0.0-alpha.3 61 3/27/2026
1.0.0-alpha.2 67 3/26/2026
1.0.0-alpha.1 72 3/8/2026