/DB

Wire up the context

Register the generated unit-of-work context in dependency injection and inject the typed factory into your services and endpoints.

updated 5 Jun 20261 min readv0.3.2View as Markdown

The generator emits a typed context for the database: an interface IAppDb with one table set per table (Projects, Tasks), plus a factory ISocigyDatabaseFactory<IAppDb> that opens connections, manages transactions, and scopes a unit of work. You never touch a DbConnection directly.

Register it

Two calls wire everything up. AddAppDb() registers the keyed connection factory and migration manager; AddAppDbContext() registers the factory you inject:

// TaskApi.Web/Program.cs
using Socigy.OpenSource.DB.Core.Context;
using Socigy.OpenSource.DB.AppDb.Extensions;
using Socigy.OpenSource.DB.AppDb.Context;

var builder = WebApplication.CreateBuilder(args);

builder.AddAppDb();                  // keyed IDbConnectionFactory + IMigrationManager
builder.Services.AddAppDbContext();  // ISocigyDatabaseFactory<IAppDb>

var app = builder.Build();
await app.EnsureLatestAppDbMigration();

The generated extensions live in Socigy.OpenSource.DB.AppDb.Extensions; the context interface lives in Socigy.OpenSource.DB.AppDb.Context.

Inject it

Inject ISocigyDatabaseFactory<IAppDb> anywhere: a minimal-API handler, a controller, or a service. The factory hands you the context inside a scoped callback:

public sealed class ProjectService(ISocigyDatabaseFactory<IAppDb> db)
{
    public Task<List<Project>> AllAsync() =>
        db.ExecuteAsync(d => d.Projects.ToListAsync());

    public Task<Project?> FindAsync(Guid id) =>
        db.ExecuteAsync(d => d.Projects.FirstOrDefaultAsync(p => p.Id == id));
}

ExecuteAsync opens one connection, runs the callback, and closes it. The d parameter is the IAppDb context, and d.Projects / d.Tasks are the typed sets. Table-set names are pluralized from the class name (Project to Projects, TaskItem to Tasks).

NOTE
Set names follow English pluralization, not a bare + "s": Category becomes Categories, Class becomes Classes. See The database context.

With the context injected, you can read and write. Continue to Read and write data.