Interface IRedbSaveInterceptor

Assembly: redb.Core.dll

Save-pipeline interceptor (discussion #12, items 3-4), deliberately shaped after EF Core's
ISaveChangesInterceptor so the behaviour is familiar:
runs after the object graph is collected and BEFORE the
transaction opens; an exception thrown here CANCELS the save and propagates to the caller —
nothing has touched the database yet. runs after the transaction has committed; an exception
propagates to the caller (exactly like EF), but the data IS saved — the save cannot be
un-committed from here.
Register any number via DI (services.AddSingleton<IRedbSaveInterceptor, ...>());
they run in registration order. Code that cannot reach that container (a module in its own
AssemblyLoadContext) subscribes at run time through ;
those run after the DI ones. With none registered and none subscribed the pipeline pays nothing —
the change feed below is not even collected.

Both hooks run INSIDE the SaveAsync call, and one scope is one save (the CT-4 guard): an
interceptor that writes its journal as redb objects through the SAME service scope will trip
that guard deterministically. Write through a separate scope (scoped IRedbService from a
scope factory), or buffer and flush outside the save.


Audit and impersonation live here by design: the application sees every object (and under
ChangeTracking every value-level change) next to whatever pair of real/effective users it
tracks, and writes its own journal - the framework does not prescribe one.


Atomic audit (discussion #12 follow-up): by default the hooks do not share the save's
transaction (Saving runs before BEGIN so a veto needs no rollback; Saved runs after COMMIT
so a hook can never unwind it). When the audit row must be atomic with the write, wrap the
call in your own transaction:
await redb.Context.ExecuteAtomicAsync(async () =>
{
await redb.SaveAsync(obj); // the whole pipeline, SavedAsync included, runs inside
});
Inside that scope an insert from SavedAsync via redb.Context.ExecuteAsync(...) (a
plain flat audit table is the recommended shape) joins the same transaction and commits or
rolls back together with the save. The same works for DeleteAsync and DeletedAsync.

public interface IRedbSaveInterceptor

Methods

DeletedAsync(RedbDeletedContext, CancellationToken)

After a hard delete committed. An exception propagates, but the rows are gone.

Task DeletedAsync(RedbDeletedContext context, CancellationToken cancellationToken = default)

DeletingAsync(RedbDeletingContext, CancellationToken)

Before a hard delete executes (owner decision: deletes are intercepted too). An exception

Task DeletingAsync(RedbDeletingContext context, CancellationToken cancellationToken = default)

SavedAsync(RedbSavedContext, CancellationToken)

Task SavedAsync(RedbSavedContext context, CancellationToken cancellationToken = default)

SavingAsync(RedbSavingContext, CancellationToken)

Task SavingAsync(RedbSavingContext context, CancellationToken cancellationToken = default)