PouryaKit
ReferencePackages

PouryaKit.Persistence.Postgres

Each Module's EF Core data in its own PostgreSQL schema.

Each Module's EF Core data in its own PostgreSQL schema. UsePouryaKitPostgres gives a Module its DbContext on Npgsql, keeps its migrations history in that schema and makes the DbContext the Module's Unit of Work. Modules may share one connection string, and each keeps its own schema.

Install

dotnet add package PouryaKit.Persistence.Postgres --prerelease

When to use it

  • You give a Module its database and schema: this is the package a Module references to store its aggregates.
  • A unique value should come back as the Error of your choice.
  • A number must be gap-free, such as one sequence of invoice numbers per year.
  • You are adding a migration.

It brings PouryaKit.Persistence and PouryaKit.Persistence.EFCore. Add PouryaKit.Persistence.Dapper for reads in SQL and PouryaKit.Messaging.Wolverine.EFCore for deliveries.

Quick start

Declare the Module's store in its Composition Root, as the Proving Sample's Catalog does:

module.UsePouryaKitPostgres<CatalogDbContext>(connectionName: "app", schema: "catalog");
module.MapPouryaKitUniqueConstraint<CatalogDbContext>(
    ProductConfiguration.NameIndex,
    ProductErrors.NameInUse
);

Number sequences take a registration of their own, and a migration of the Module that calls CreateNumberSequenceTable with its schema, or the first number throws:

module.AddPouryaKitNumberSequences();

A command asks for the next number inside its Unit of Work:

var number = await sequences.NextAsync($"stock-count:{year}", cancellationToken);

What you get

  • A schema per Module, with its own migrations history, on one or several databases.
  • A violated unique index read back as the Error the Module mapped.
  • Gap-free numbers per scope key, given back when the Operation rolls back.

The Host applies no migration at startup. Apply each Module's migrations yourself; in tests, HostFixture of PouryaKit.Testing applies them.

Learn more

On this page