RoomSharp.DependencyInjection
0.5.0
See the version list below for details.
dotnet add package RoomSharp.DependencyInjection --version 0.5.0
NuGet\Install-Package RoomSharp.DependencyInjection -Version 0.5.0
<PackageReference Include="RoomSharp.DependencyInjection" Version="0.5.0" />
<PackageVersion Include="RoomSharp.DependencyInjection" Version="0.5.0" />
<PackageReference Include="RoomSharp.DependencyInjection" />
paket add RoomSharp.DependencyInjection --version 0.5.0
#r "nuget: RoomSharp.DependencyInjection, 0.5.0"
#:package RoomSharp.DependencyInjection@0.5.0
#addin nuget:?package=RoomSharp.DependencyInjection&version=0.5.0
#tool nuget:?package=RoomSharp.DependencyInjection&version=0.5.0
RoomSharp.DependencyInjection
Dependency Injection extensions for integrating RoomSharp databases with IServiceCollection using familiar ASP.NET Core patterns.
Installation
dotnet add package RoomSharp.DependencyInjection
Quick Start
var builder = WebApplication.CreateBuilder(args);
// Register database
builder.Services.AddRoomSharpDatabase<AppDatabase>(context =>
{
context.UseSqlite("app.db");
context.Builder
.SetVersion(1)
.AddMigrations(new InitialMigration());
});
// Register DAOs — manually
builder.Services.AddRoomSharpDao<AppDatabase, ITodoDao>(db => db.TodoDao);
// Or auto-register all DAOs at once
builder.Services.AddRoomSharpDaos<AppDatabase>();
Provider Shortcuts
Configure your database provider directly from the builder context:
// SQLite
context.UseSqlite("app.db");
context.UseSqliteInMemory();
context.UseSqliteWithConnectionString("Data Source=app.db;Mode=ReadWriteCreate");
// SQL Server (requires RoomSharp.SqlServer)
context.UseSqlServer("Server=.;Database=MyApp;Trusted_Connection=True;");
// PostgreSQL (requires RoomSharp.PostgreSql)
context.UsePostgres("Host=localhost;Database=myapp;Username=user;Password=pass;");
// MySQL/MariaDB (requires RoomSharp.MySql)
context.UseMySql("Server=localhost;Database=myapp;User=root;Password=pass;");
// Custom provider resolved from DI
context.UseProvider<MyCustomProvider>("connection-string");
Encryption & Audit Setup
Configure [Encrypted] and [Auditable] features directly from the DI builder:
builder.Services.AddRoomSharpDatabase<AppDatabase>(context =>
{
context.UseSqlite("app.db");
// Encryption — pass an instance
context.SetEncryptionProvider(new AesEncryptionProvider(key));
// Or resolve IEncryptionProvider from the DI container
context.SetEncryptionProviderFromServices();
// Audit — pass an instance
context.SetAuditContext(new MyAuditContext(userId));
// Or resolve IAuditContext from the DI container
context.SetAuditContextFromServices();
});
Service Lifetime
Control the database and DAO lifetimes:
// Singleton (default)
builder.Services.AddRoomSharpDatabase<AppDatabase>(
ctx => ctx.UseSqlite("app.db"));
// Scoped — new instance per request
builder.Services.AddRoomSharpDatabase<AppDatabase>(
ctx => ctx.UseSqlite("app.db"),
ServiceLifetime.Scoped);
// Shorthand for scoped
builder.Services.AddRoomSharpScopedDatabase<AppDatabase>(
ctx => ctx.UseSqlite("app.db"));
Captive Dependency Protection: RoomSharp validates lifetime compatibility. A Singleton DAO with a Scoped database will throw
InvalidOperationExceptionat registration time.
Database Factory
For background services, Blazor Server, or manual lifecycle control:
builder.Services.AddRoomSharpDatabaseFactory<AppDatabase>(ctx =>
ctx.UseSqlite("app.db"));
// Usage in a background service
public class MyWorker : BackgroundService
{
private readonly IRoomDatabaseFactory<AppDatabase> _factory;
public MyWorker(IRoomDatabaseFactory<AppDatabase> factory)
=> _factory = factory;
protected override async Task ExecuteAsync(CancellationToken token)
{
using var db = _factory.Create();
var todos = await db.TodoDao.GetAllAsync();
}
}
Configuration-Based Registration
Bind connection strings from IConfiguration:
builder.Services.AddRoomSharpDatabase<AppDatabase>(
configuration,
connectionStringName: "Default",
providerFactory: sp => new SqlServerProvider(),
configure: b => b.SetVersion(2));
Health Checks
Register a health check that verifies actual database connectivity via SELECT 1:
builder.Services.AddHealthChecks()
.AddRoomSharpDatabase<AppDatabase>();
// With custom name and tags
builder.Services.AddHealthChecks()
.AddRoomSharpDatabase<AppDatabase>(
name: "app-db",
tags: new[] { "db", "critical" });
Keyed Services (.NET 8+)
Register multiple database instances with different keys:
builder.Services.AddKeyedRoomSharpDatabase<AppDatabase>("primary",
ctx => ctx.UseSqlite("primary.db"));
builder.Services.AddKeyedRoomSharpDatabase<AppDatabase>("secondary",
ctx => ctx.UsePostgres(secondaryConnStr));
// Resolve
var db = sp.GetRequiredKeyedService<AppDatabase>("primary");
API Reference
Service Collection Extensions
| Method | Description |
|---|---|
AddRoomSharpDatabase<T>(configure, lifetime?) |
Register database with builder callback |
AddRoomSharpDatabase<T>(context, lifetime?) |
Register with access to IServiceProvider |
AddRoomSharpDatabase<T>(config, connName, ...) |
Bind from IConfiguration |
AddRoomSharpScopedDatabase<T>(configure) |
Shorthand for scoped registration |
AddRoomSharpDatabaseFactory<T>(configure) |
Register factory for manual lifecycle |
AddRoomSharpDao<TDb, TDao>(factory, lifetime?) |
Register a single DAO with factory + captive dependency validation |
AddRoomSharpDaos<TDb>(lifetime?) |
Auto-discover and register all abstract DAO properties from the database |
AddKeyedRoomSharpDatabase<T>(key, configure, lifetime?) |
Register keyed database (.NET 8+) |
AddKeyedRoomSharpDao<TDb, TDao>(key, dbKey, factory, lifetime?) |
Register keyed DAO (.NET 8+) |
Builder Context Methods
| Method | Description |
|---|---|
UseSqlite(path) |
Configure SQLite with file path |
UseSqliteInMemory() |
Configure SQLite in-memory |
UseSqliteWithConnectionString(connStr) |
Configure SQLite with full connection string |
UseSqlServer(connStr) |
Configure SQL Server |
UsePostgres(connStr) |
Configure PostgreSQL |
UseMySql(connStr) |
Configure MySQL/MariaDB |
UseProvider<T>(connStr) |
Resolve custom provider from DI |
UseProvider(provider, connStr) |
Use externally constructed provider |
SetEncryptionProvider(provider) |
Set encryption for [Encrypted] properties |
SetEncryptionProviderFromServices() |
Resolve IEncryptionProvider from DI |
SetAuditContext(context) |
Set audit context for [Auditable] entities |
SetAuditContextFromServices() |
Resolve IAuditContext from DI |
Health Check Extensions
| Method | Description |
|---|---|
AddRoomSharpDatabase<T>(name?, failureStatus?, tags?) |
Register health check with real DB ping |
| 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 is compatible. 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 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. |
-
net10.0
- Microsoft.Extensions.Configuration.Abstractions (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Diagnostics.HealthChecks (>= 8.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.0)
- RoomSharp (>= 0.5.0)
-
net8.0
- Microsoft.Extensions.Configuration.Abstractions (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Diagnostics.HealthChecks (>= 8.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.0)
- RoomSharp (>= 0.5.0)
-
net9.0
- Microsoft.Extensions.Configuration.Abstractions (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Diagnostics.HealthChecks (>= 8.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.0)
- RoomSharp (>= 0.5.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
v0.5.0
New Features:
- AddRoomSharpDaos<TDatabase>(): Auto-register all DAOs for a RoomSharp database in a single call.
- SetEncryptionProvider / SetEncryptionProviderFromServices: Configure [Encrypted] property encryption directly from the DI builder context. Supports both explicit provider instances and automatic resolution from IServiceCollection.
- SetAuditContext / SetAuditContextFromServices: Configure [Auditable] CreatedBy tracking directly from the DI builder context. Supports both explicit IAuditContext instances and automatic resolution from IServiceCollection.
- RoomDatabaseBuilder now exposes public SetEncryptionProvider() and SetAuditContext() methods for non-DI usage as well.
Improved:
- Health Check now performs a real database connectivity test (SELECT 1) instead of only verifying that the DI instance exists. This catches connection failures, disposed databases, and network issues.