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

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.

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>