API reference — Persistence
Nuvora.Nexus.Relay.Persistence.EfCore
EF Core unit of work and event-sourced repository for Nuvora Nexus Relay
dotnet add package Nuvora.Nexus.Relay.Persistence.EfCore
Nuvora.Nexus.Relay.Persistence.EfCore
EfCoreEventSourcedReader<TAggregate, TId>
public sealed class EfCoreEventSourcedReader<TAggregate, TId> : IEventSourcedReader<TAggregate, TId> where TAggregate : AggregateRoot<TId> where TId : notnull
IEventSourcedReader over the event store. Reads the aggregate’s stream and folds the (optionally filtered) events into a fresh instance via AggregateRoot.LoadFromHistory. Snapshots are deliberately ignored: a snapshot captures the head, which would be wrong for a time-travel read, and replaying from zero is correct (and acceptable for read-side queries) — provided the stream’s full history is still live. A stream whose early events were moved to relay_events_archive cannot be folded from zero, so the reader fails fast instead of silently returning wrong state. The result is not tracked on the unit of work — these are pure reads.
Constructors
EfCoreEventSourcedReader(IEventStore eventStore, IEventSerializer serializer)
Methods
Task<TAggregate?> LoadAsync(TId id, CancellationToken cancellationToken = default(CancellationToken))
Rehydrates the aggregate’s current state by replaying its whole stream (live aggregation). Returns null if the aggregate has no events.
Task<TAggregate?> LoadAtTimeAsync(TId id, DateTimeOffset asOf, CancellationToken cancellationToken = default(CancellationToken))
Rehydrates the aggregate as it stood at asOf (events whose timestamp is at or before it). Returns null if it had no events by then.
Task<TAggregate?> LoadAtVersionAsync(TId id, long version, CancellationToken cancellationToken = default(CancellationToken))
Rehydrates the aggregate as of (and including) version. Returns null if it had no events at or below that version.
EfCoreEventSourcedRepository<TAggregate, TId>
public sealed class EfCoreEventSourcedRepository<TAggregate, TId> : IEventSourcedRepository<TAggregate, TId> where TAggregate : AggregateRoot<TId> where TId : notnull
Event-sourced repository over the EF Core event store. Rehydrates an aggregate from its latest snapshot (when ISnapshotable) plus the events after it, else a full replay. Saving only tracks the aggregate on the unit of work — the transaction behavior appends and dispatches the uncommitted events atomically at commit, so there is a single write path.
Constructors
EfCoreEventSourcedRepository(IEventStore eventStore, ISnapshotStore snapshotStore, IEventSerializer serializer, IUnitOfWork unitOfWork)
Methods
Task<TAggregate?> GetByIdAsync(TId id, CancellationToken cancellationToken = default(CancellationToken))
Rehydrates the aggregate, or returns null if it has no events.
void Add(TAggregate aggregate)
Tracks a new aggregate so its events are appended at commit.
void Update(TAggregate aggregate)
Tracks a loaded aggregate so its new events are appended at commit.
EfCoreRepository<TEntity, TId>
public sealed class EfCoreRepository<TEntity, TId> : IRepository<TEntity, TId> where TEntity : Entity<TId> where TId : notnull
EF Core implementation of IRepository over the command’s shared DbContext (via IRelayDbContextAccessor). Writes are staged only; the unit of work commits them. Predicate queries take Expression so they translate to SQL instead of loading the whole table and filtering in memory.
Constructors
EfCoreRepository(IRelayDbContextAccessor contextAccessor)
Methods
Task AddAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken))
Add entity
Task AddRangeAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken = default(CancellationToken))
Add multiple entities
Task RemoveAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken))
Remove entity
Task RemoveAsync(TId id, CancellationToken cancellationToken = default(CancellationToken))
Remove entity by ID
Task RemoveRangeAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken = default(CancellationToken))
Remove multiple entities
Task RemoveWhereAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default(CancellationToken))
Remove entities by predicate
Task UpdateAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken))
Update entity
Task UpdateRangeAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken = default(CancellationToken))
Update multiple entities
Task<IEnumerable<TEntity>> GetAllAsync(CancellationToken cancellationToken = default(CancellationToken))
Get all entities
Task<IEnumerable<TEntity>> GetWhereAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default(CancellationToken))
Get entities by predicate
Task<TEntity?> GetByIdAsync(TId id, CancellationToken cancellationToken = default(CancellationToken))
Get entity by ID
Task<bool> ExistsAsync(TId id, CancellationToken cancellationToken = default(CancellationToken))
Check if entity exists by ID
Task<int> CountAsync(CancellationToken cancellationToken = default(CancellationToken))
Count entities
Task<int> CountAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default(CancellationToken))
Count entities by predicate
EfCoreUnitOfWork<TContext>
public class EfCoreUnitOfWork<TContext> : IUnitOfWork where TContext : DbContext
Framework-provided IUnitOfWork over an EF Core TContext. Event appends, outbox rows and aggregate state are all staged on this one context, so EfCoreUnitOfWork.CommitAsync flushes and commits them in a single transaction. Optimistic-concurrency conflicts from the event store’s unique index surface here as a DbUpdateException and are translated to a typed ConcurrencyConflictException. Registered scoped (one per command).
Constructors
EfCoreUnitOfWork(TContext context, IRelayTenantTransactionHook? tenantHook = null, ILogger<EfCoreUnitOfWork<TContext>>? logger = null)
Properties
bool HasActiveTransaction { get; }
Check if a transaction is active
Methods
IEnumerable<object> GetTrackedAggregates()
Get all tracked aggregates
Task BeginTransactionAsync(CancellationToken cancellationToken = default(CancellationToken))
Begin a new transaction
Task CommitAsync(CancellationToken cancellationToken = default(CancellationToken))
Commit the current transaction and save changes
Task RollbackAsync(CancellationToken cancellationToken = default(CancellationToken))
Rollback the current transaction
Task<int> SaveChangesAsync(CancellationToken cancellationToken = default(CancellationToken))
Save changes without committing transaction
void TrackAggregate<TId>(AggregateRoot<TId> aggregate)
Track an aggregate for event persistence and dispatch
IRelayTenantTransactionHook
public interface IRelayTenantTransactionHook
Soft-dependency hook the unit of work calls immediately after opening the command transaction, so a tenant storage strategy can scope it to the ambient tenant (e.g. SET LOCAL app.current_tenant for shared-table RLS). Kept here — below the tenancy packages — so the UoW has no compile edge to Relay.Tenancy.EfCore; the default is a no-op and tenancy replaces it.
Methods
Task OnTransactionBeganAsync(DbContext context, CancellationToken cancellationToken = default(CancellationToken))
Called once per command transaction, just after it begins, before any work runs.
NullRelayTenantTransactionHook
public sealed class NullRelayTenantTransactionHook : IRelayTenantTransactionHook
Default no-op hook: the command transaction is not tenant-scoped.
Methods
Task OnTransactionBeganAsync(DbContext context, CancellationToken cancellationToken = default(CancellationToken))
Called once per command transaction, just after it begins, before any work runs.
Nuvora.Nexus.Relay.Persistence.EfCore.DependencyInjection
PersistenceServiceCollectionExtensions
public static class PersistenceServiceCollectionExtensions
Registration helpers for the EF Core unit of work and event-sourced repository, bound to the host’s TContext so they share the command’s transaction.
Methods
static IServiceCollection AddRelayAggregateWriteLock(this IServiceCollection services)
Registers IAggregateWriteLock (pessimistic per-aggregate write locking) backed by the registered IDistributedLock. Requires a distributed lock — the EF Core event store (AddRelayEventStoreEfCore) registers the PostgreSQL advisory-lock implementation. Opt-in: acquire the lock inside a command before loading a hot aggregate to serialize concurrent writers.
static IServiceCollection AddRelayEventSourcedReaderEfCore(this IServiceCollection services)
Registers the open-generic read-only IEventSourcedReader for live aggregation and time-travel reads. Requires the event store (AddRelayEventStoreEfCore). Registered automatically by PersistenceServiceCollectionExtensions.AddRelayEventSourcedRepositoryEfCore.
static IServiceCollection AddRelayEventSourcedRepositoryEfCore(this IServiceCollection services)
Registers the open-generic event-sourced repository. Requires the event store (AddRelayEventStoreEfCore) and the unit of work (PersistenceServiceCollectionExtensions.AddRelayUnitOfWorkEfCore).
static IServiceCollection AddRelayRepositoryEfCore(this IServiceCollection services)
Registers the open-generic CRUD repository IRepository backed by the command’s shared DbContext. Requires the event-store accessor (AddRelayEventStoreEfCore).
static IServiceCollection AddRelayUnitOfWorkEfCore<TContext>(this IServiceCollection services)
Registers EfCoreUnitOfWork as the scoped IUnitOfWork. Frees consumers from hand-rolling the unit of work; the transaction behavior drives it.