To help with using Dapper in the Clean Architecture project structure, there are two interfaces provided in this example that can be used to simplify connection and transaction management in MediatR requests within the Application project.
IDbReadContext
- Injects a DbConnection to be used in Read Only scenariosIDbContext
- Injects a DbConnection within an open transaction to allow grouping of data altering requests and Domain event publishing
The main web project will spin up a postgres container using the Testcontainers package to not have to depend external setup for the database.
public interface IDbContext
{
/// <summary>
/// Connection tied to a transaction that is not committed until SaveChangesAsync is called
/// </summary>
public IDbConnection Connection { get; }
/// <summary>
/// Add events to a temporary collection to be fired once all changes have been successfully saved
/// </summary>
void AddEvent(IDomainEvent domainEvent);
/// <summary>
/// Commit in progress Database transaction and fire any pending events after successful commit
/// </summary>
Task SaveChangesAsync(CancellationToken token = default);
}
public interface IDbReadContext
{
public IDbConnection Connection { get; }
}
https://github.com/vrajmohan/pgsql-sample-data/tree/master/employee