Skip to main content
Table of Contents

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

TDbContext

The concrete DbContext type owned by this unit of work.

Inheritance
UnitOfWork<TDbContext>
Implements
Derived
Inherited Members
Extension Methods

Remarks

A save the store committed is never reported as cancelled. On a context whose options carry CommitCompletionInterceptor — every context the framework registers — a transaction's save honours the caller's token while the changes are written and lets the commit run to its end. On any other — a context a host registered itself without the interceptor, one whose interceptors come from an internal service provider, or one whose AutoTransactionBehavior is Never — the save ignores the token and runs to its end whole, bounded by the connection's pool wait and command timeout rather than by the caller; a host that stops within a shorter shutdown timeout adds the interceptor to such a context.

On a context whose execution strategy retries on failure, a transaction's save that does not already run inside a transaction runs as one retriable unit through that strategy: a transaction of its own, the changes written with SaveChangesAsync(false, …) and accepted only once the commit has run, so a transient failure — the commit's included — runs the whole save again. On such a context an override of SaveChangesAsync(CancellationToken) alone is not called, SavedChanges handlers run before the commit, and a single statement is saved in a transaction.

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

contextFactory IDbContextFactory<TDbContext>

Factory used to create a new DbContext per transaction.

Remarks

A save the store committed is never reported as cancelled. On a context whose options carry CommitCompletionInterceptor — every context the framework registers — a transaction's save honours the caller's token while the changes are written and lets the commit run to its end. On any other — a context a host registered itself without the interceptor, one whose interceptors come from an internal service provider, or one whose AutoTransactionBehavior is Never — the save ignores the token and runs to its end whole, bounded by the connection's pool wait and command timeout rather than by the caller; a host that stops within a shorter shutdown timeout adds the interceptor to such a context.

On a context whose execution strategy retries on failure, a transaction's save that does not already run inside a transaction runs as one retriable unit through that strategy: a transaction of its own, the changes written with SaveChangesAsync(false, …) and accepted only once the commit has run, so a transient failure — the commit's included — runs the whole save again. On such a context an override of SaveChangesAsync(CancellationToken) alone is not called, SavedChanges handlers run before the commit, and a single statement is saved in a 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

transaction ITransaction

A transaction created by this unit of work.

Returns

TDbContext

The DbContext that owns the transaction's change tracker.

Exceptions

ArgumentException

Thrown when transaction was not created by this unit of work.

StartAsync(CancellationToken)

Open a new transaction asynchronously.

public Task<ITransaction> StartAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Returns

Task<ITransaction>