Shared : Infrastructure.Persistence

The Shared.Infrastructure.Persistence project provides the persistence layer abstractions and implementations built on top of Entity Framework Core. It includes base repository classes for command, query, and domain operations, a Unit of Work implementation, a domain event publishing interceptor, and dependency injection helpers for database context and service registration.

Abstractions : Repositories

The repository abstractions provide base classes for data access patterns using Entity Framework Core :
  • CommandRepository<TDbContext, TEntity, TEntityId> : An abstract base class for write-side repositories. It provides Add, Update, Remove, and GetEntityAsync methods that operate on the DbContext directly.
  • QueryRepository<TDbContext> : An abstract base class for read-side repositories. It exposes an IDbContextFactory for creating short-lived, isolated read contexts.
  • DomainRepository<TDbContext> : An abstract base class for domain-level data checks. It provides ExistsWithPropertyNameAsync and ExistsWithPropertyNameExcludingIdAsync methods for uniqueness validation, using a context factory for each query.

Dependency Injection

The InfrastructurePersistenceDependencyInjection static class provides helper methods to register persistence services into the DI container :
  • AddDbContextFactory : Registers an IDbContextFactory for a given DbContext type, configured with SQL Server and connection resiliency (retry on failure). Supports optional additional configuration via a callback.
  • AddDbContextFactoryWithDomainEvents : Extends the above by registering the PublishDomainEventsInterceptor as a singleton and adding it to the DbContext options, enabling automatic domain event dispatch on save.
  • AddUnitOfWork : Registers the UnitOfWork<TDbContext> as scoped, binding it to both IUnitOfWork and the DbContext so that all scoped services share the same context instance.
  • AddCommandRepositories / AddQueryRepositories / AddDomainRepositories : Registers all concrete types implementing the respective marker interfaces by scanning a given assembly.
  • AddDomainServices : Registers all concrete types implementing IDomainService by scanning a given assembly.

Interceptors

The PublishDomainEventsInterceptor is an internal sealed SaveChangesInterceptor that automatically collects and dispatches domain events during SaveChanges. Before saving, it scans the ChangeTracker for entities implementing IEntity, collects their domain events, and clears them from the entities. After saving, it resolves an IDomainEventPublisher from a new service scope and publishes each event using cached reflection method info for performance.

Unit of Work

The UnitOfWork<TDbContext> is an internal sealed class implementing IUnitOfWork and IAsyncDisposable. It lazily creates a DbContext from an IDbContextFactory, exposes a SaveChangesAsync method to persist changes, and disposes the context asynchronously when the unit of work scope ends.

Rejoining the server...

Rejoin failed... trying again in seconds.

Failed to rejoin.
Please retry or reload the page.

The session has been paused by the server.

Failed to resume the session.
Please reload the page.