Class UnitOfWork<TDbContext>
- Namespace
- Stratara.EventSourcing.EntityFrameworkCore
- Assembly
- Stratara.EventSourcing.EntityFrameworkCore.dll
Base IUnitOfWork implementation backed by an EF Core IDbContextFactory<TContext>. Each StartAsync(CancellationToken) call mints a fresh DbContext that lives for the duration of the returned transaction.
public class UnitOfWork<TDbContext> : IUnitOfWork where TDbContext : DbContext, IDbContext
Type Parameters
TDbContextThe concrete DbContext type owned by this unit of work.
- Inheritance
-
UnitOfWork<TDbContext>
- Implements
- Derived
- Inherited Members
- Extension Methods
Constructors
UnitOfWork(IDbContextFactory<TDbContext>)
Base IUnitOfWork implementation backed by an EF Core IDbContextFactory<TContext>. Each StartAsync(CancellationToken) call mints a fresh DbContext that lives for the duration of the returned transaction.
public UnitOfWork(IDbContextFactory<TDbContext> contextFactory)
Parameters
contextFactoryIDbContextFactory<TDbContext>Factory used to create a new DbContext per transaction.
Methods
GetDbContext(ITransaction)
Extracts the underlying TDbContext from a transaction previously
returned by StartAsync(CancellationToken). Used by derived unit-of-work classes when wiring
their repositories.
protected static TDbContext GetDbContext(ITransaction transaction)
Parameters
transactionITransactionA transaction created by this unit of work.
Returns
- TDbContext
The DbContext that owns the transaction's change tracker.
Exceptions
- ArgumentException
Thrown when
transactionwas not created by this unit of work.
StartAsync(CancellationToken)
Open a new transaction asynchronously.
public Task<ITransaction> StartAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationToken