Muonroi.Data.Dapper
1.0.0-alpha.16
dotnet add package Muonroi.Data.Dapper --version 1.0.0-alpha.16
NuGet\Install-Package Muonroi.Data.Dapper -Version 1.0.0-alpha.16
<PackageReference Include="Muonroi.Data.Dapper" Version="1.0.0-alpha.16" />
<PackageVersion Include="Muonroi.Data.Dapper" Version="1.0.0-alpha.16" />
<PackageReference Include="Muonroi.Data.Dapper" />
paket add Muonroi.Data.Dapper --version 1.0.0-alpha.16
#r "nuget: Muonroi.Data.Dapper, 1.0.0-alpha.16"
#:package Muonroi.Data.Dapper@1.0.0-alpha.16
#addin nuget:?package=Muonroi.Data.Dapper&version=1.0.0-alpha.16&prerelease
#tool nuget:?package=Muonroi.Data.Dapper&version=1.0.0-alpha.16&prerelease
Muonroi.Data.Dapper
Dapper integration for Muonroi: lightweight read-side repository, multi-tenant query filtering, and connection factory.
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>:ConnectionStringfromIConfigurationand implementsIConnectionStringProviderfor Dapper.Extensions.NetCore.MDapperCommand— command builder (CommandText,Parameters,Transaction,CommandType,CommandFlags) with aBuild(CancellationToken)method that produces aCommandDefinition.MSqlMapperTypeExtensions.RegisterDapperHandlers()— registersMProtobufTimestampHandler(Google ProtobufTimestamp) andMTrimStringHandler(auto-trim strings) as globalSqlMappertype handlers.AddMuonroiDapperRls()— replaces the liveIDapperwithTenantRlsDapper<TConn>whenMultiTenantConfigs.EnableRowLevelSecurityistrue; 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 (Nativefor PostgreSQL/MSSQL).- Startup verifier (
RlsStartupVerifier) — hosted service that asserts required RLS DDL objects exist on startup; configurable viaVerifyRlsObjectsOnStartup. - Strict mode (
StrictMode = true) — throwsMissingTenantContextExceptionat 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)
Related Packages
Muonroi.Data.Abstractions—IConnectionStringProviderand core data contracts consumed by this packageMuonroi.Tenancy.Abstractions—ITenantContextandMultiTenantOptions(includingEnableRowLevelSecurity) consumed by the RLS layerMuonroi.Core—MGuard, logging, and exception base types used internally
License
Apache-2.0. See LICENSE-APACHE.
| Product | Versions 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. |
-
net8.0
- Dapper (>= 2.1.66)
- Dapper.Extensions.NetCore (>= 5.3.1)
- Google.Protobuf (>= 3.29.1)
- Microsoft.Data.SqlClient (>= 5.1.7)
- Microsoft.EntityFrameworkCore (>= 8.0.24)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.3)
- Muonroi.Core (>= 1.0.0-alpha.16)
- Muonroi.Core.Abstractions (>= 1.0.0-alpha.16)
- Muonroi.Data.Abstractions (>= 1.0.0-alpha.16)
- Muonroi.Logging.Abstractions (>= 1.0.0-alpha.16)
- Muonroi.Tenancy.Abstractions (>= 1.0.0-alpha.16)
- Npgsql (>= 8.0.9)
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 |