Dapper.AmbientContext 2.1.0

dotnet add package Dapper.AmbientContext --version 2.1.0
                    
NuGet\Install-Package Dapper.AmbientContext -Version 2.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Dapper.AmbientContext" Version="2.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Dapper.AmbientContext" Version="2.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Dapper.AmbientContext" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Dapper.AmbientContext --version 2.1.0
                    
#r "nuget: Dapper.AmbientContext, 2.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Dapper.AmbientContext@2.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Dapper.AmbientContext&version=2.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Dapper.AmbientContext&version=2.1.0
                    
Install as a Cake Tool

Dapper.AmbientContext

CI/CD NuGet License: MIT

Ambient context implementation for Dapper with automatic transaction management.

Overview

Dapper.AmbientContext implements the ambient context pattern to seamlessly manage database connections and transactions across multiple data access components. It allows service layers to compose multiple repositories, commands, and queries that automatically participate in the same transaction without explicit connection or transaction passing.

Key Features

  • Automatic transaction management - Share connections and transactions across components
  • Thread-safe - Safe for concurrent operations
  • Nested contexts - Support for parent-child context hierarchies
  • Cross-platform - Works on .NET Framework, .NET Core, and .NET 5+

Installation

Install via NuGet:

dotnet add package Dapper.AmbientContext

Or using Package Manager Console:

Install-Package Dapper.AmbientContext

Requirements

  • .NET Framework 4.6.2 or higher
  • .NET Core 2.0 or higher
  • .NET 5.0 or higher

Note: For .NET Framework 4.5.1 - 4.6.1 support, use version 1.8.1.

Quick Start

1. Set up your connection factory

public class SqlConnectionFactory : IDbConnectionFactory
{
    private readonly string _connectionString;

    public SqlConnectionFactory(string connectionString)
    {
        _connectionString = connectionString;
    }

    public IDbConnection Create()
    {
        return new SqlConnection(_connectionString);
    }
}

2. Register services in your DI container

using Dapper.AmbientContext.Extensions;

// With connection factory instance
services.AddDapperAmbientContext(new SqlConnectionFactory(connectionString));

// OR with connection factory type
services.AddDapperAmbientContext<SqlConnectionFactory>(connectionString);

// Register your repositories
services.AddTransient<IUserRepository, UserRepository>();
services.AddTransient<IOrderRepository, OrderRepository>();

3. Create a repository

public class UserRepository : IUserRepository
{
    private readonly IAmbientDbContextLocator _locator;

    public UserRepository(IAmbientDbContextLocator locator)
    {
        _locator = locator;
    }

    public async Task<User> GetByIdAsync(int id)
    {
        var context = _locator.Get();
        var prepared = await context.PrepareAsync();
        
        return await prepared.Connection.QuerySingleOrDefaultAsync<User>(
            "SELECT * FROM Users WHERE Id = @Id",
            new { Id = id },
            transaction: prepared.Transaction);
    }

    public async Task CreateAsync(User user)
    {
        var context = _locator.Get();
        var prepared = await context.PrepareAsync();
        
        await prepared.Connection.ExecuteAsync(
            "INSERT INTO Users (Name, Email) VALUES (@Name, @Email)",
            user,
            transaction: prepared.Transaction);
    }
}

4. Use in your service layer

public class UserService
{
    private readonly IAmbientDbContextFactory _contextFactory;
    private readonly IUserRepository _userRepository;
    private readonly IOrderRepository _orderRepository;

    public UserService(
        IAmbientDbContextFactory contextFactory,
        IUserRepository userRepository,
        IOrderRepository orderRepository)
    {
        _contextFactory = contextFactory;
        _userRepository = userRepository;
        _orderRepository = orderRepository;
    }

    public async Task<User> CreateUserWithOrderAsync(User user, Order order)
    {
        // Create ambient context - both repositories will share this transaction
        using (var context = _contextFactory.Create())
        {
            await _userRepository.CreateAsync(user);
            await _orderRepository.CreateAsync(order);
            
            // Transaction is automatically committed on Dispose if no exceptions
            return user;
        }
    }
}

Advanced Usage

Nested Contexts

using (var parentContext = _contextFactory.Create())
{
    await _userRepository.CreateAsync(user);
    
    // Child context joins parent's transaction
    using (var childContext = _contextFactory.Create(join: true))
    {
        await _orderRepository.CreateAsync(order);
        // Child dispose doesn't commit - parent owns the transaction
    }
    
    // Transaction committed here when parent is disposed
}

Transaction Suppression

// Create context without a transaction
using (var context = _contextFactory.Create(suppress: true))
{
    // Queries run without transaction
    var users = await _userRepository.GetAllAsync();
}

Custom Isolation Level

using (var context = _contextFactory.Create(
    isolationLevel: IsolationLevel.Serializable))
{
    await _userRepository.CreateAsync(user);
}

Manual Commit/Rollback

using (var context = _contextFactory.Create())
{
    try
    {
        await _userRepository.CreateAsync(user);
        context.Commit(); // Explicit commit
    }
    catch
    {
        context.Rollback(); // Explicit rollback
        throw;
    }
}

How It Works

The ambient context pattern uses AsyncLocal (.NET Core/5+) or LogicalCallContext (.NET Framework) to maintain a stack of database contexts. When you create a context:

  1. A new connection is opened (or inherited from parent)
  2. A transaction is started (unless suppressed)
  3. Context is pushed onto the ambient stack
  4. All data access components get the current context via IAmbientDbContextLocator
  5. On dispose, the context is popped and transaction is committed/rolled back

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

Built on top of Dapper - the simple object mapper for .NET.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.1.0 81 4/18/2026
2.0.2 85 4/18/2026
1.8.1 66,535 7/24/2019
1.7.0 1,432 3/17/2019
1.6.0 7,725 7/17/2018
1.5.0 73,560 3/19/2018
1.4.0 2,439 2/9/2018
1.3.0 2,185 8/1/2017
1.0.37 2,239 11/28/2016
1.0.35 1,967 11/26/2016
1.0.27 1,880 10/11/2016