Class UnitOfWorkService
Npgsql implementation of IUnitOfWorkService.
public sealed class UnitOfWorkService : IUnitOfWorkService
- Inheritance
-
UnitOfWorkService
- Implements
- Inherited Members
Remarks
This service begins an explicit database transaction and establishes an ambient
NpgsqlDbContext for the current logical async flow.
Any Npgsql service method invoked inside the unit of work will reuse the ambient context
via DatabaseExecutionScope and therefore participate in the same transaction.
The transaction is finalized only when CommitAsync(CancellationToken) or RollbackAsync(CancellationToken) is called.
Raw SQL statements execute immediately against the database connection but remain uncommitted until the unit of work commits (or are discarded on rollback).
Constructors
UnitOfWorkService(INpgsqlDbContextFactory)
Initializes a new instance of the UnitOfWorkService class.
public UnitOfWorkService(INpgsqlDbContextFactory dbContextFactory)
Parameters
dbContextFactoryINpgsqlDbContextFactoryFactory for creating database contexts.
Exceptions
- ArgumentNullException
Thrown when dbContextFactory is null.
Methods
BeginAsync(CancellationToken)
Begins a new unit of work for the current logical async flow.
public Task<DatabaseResult<IUnitOfWork>> BeginAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
- Task<DatabaseResult<IUnitOfWork>>
A DatabaseResult<T> containing an active IUnitOfWork on success.
Remarks
The returned unit of work establishes an ambient NpgsqlDbContext.
Service calls made within the scope will reuse that context and transaction.