EdShel.DapperBatcher 1.0.1

dotnet add package EdShel.DapperBatcher --version 1.0.1
                    
NuGet\Install-Package EdShel.DapperBatcher -Version 1.0.1
                    
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="EdShel.DapperBatcher" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="EdShel.DapperBatcher" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="EdShel.DapperBatcher" />
                    
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 EdShel.DapperBatcher --version 1.0.1
                    
#r "nuget: EdShel.DapperBatcher, 1.0.1"
                    
#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 EdShel.DapperBatcher@1.0.1
                    
#: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=EdShel.DapperBatcher&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=EdShel.DapperBatcher&version=1.0.1
                    
Install as a Cake Tool

DapperBatcher

Lightweight & easy to use .NET library for batching database commands made through Dapper.

Installation

The package is available on nuget

EdShel.DapperBatcher

dotnet add package EdShel.DapperBatcher

How to use

See examples project for the full code.

using EdShel.DapperBatcher; // Import the namespace
using Microsoft.Data.Sqlite;

using var connection = new SqliteConnection("Data Source=:memory:");
connection.Open(); // As with regular Dapper, you may omit opening the connection, but because it's an in-memory DB, we want to call Open() explicitly
InitializeDB(connection);

// Call *Batched methods - they will not execute immediately
IBatchedValue<Cat> val1 = connection.QueryFirstBatched<Cat>("SELECT Id, Name, CoatColor FROM Cat WHERE Id = $Id", new { Id = 1 });
IBatchedValue<Cat?> val2 = connection.QueryFirstOrDefaultBatched<Cat>("SELECT Id, Name, CoatColor FROM Cat WHERE Id = $Id", new { Id = 2 });
IBatchedValue<Cat> val3 = connection.QuerySingleBatched<Cat>("SELECT Id, Name, CoatColor FROM Cat WHERE Id = $Id", new { Id = 3 });
IBatchedValue<Cat?> val4 = connection.QuerySingleOrDefaultBatched<Cat>("SELECT Id, Name, CoatColor FROM Cat WHERE Id = $Id", new { Id = 222 });
IBatchedValue<int> affectedRows1 = connection.ExecuteBatched("INSERT INTO Cat (Id, Name, CoatColor) VALUES ($Id, $Name, $CoatColor)", new { Id = 6, Name = "Snowball", CoatColor = "White" });
IBatchedValue<int> affectedRows2 = connection.ExecuteBatched("INSERT INTO Cat (Id, Name, CoatColor) VALUES ($Id, $Name, $CoatColor)", new { Id = 7, Name = "Snowball 2", CoatColor = "Black" });
IBatchedValue<IEnumerable<Cat>> allCats = connection.QueryBatched<Cat>("SELECT Id, Name, CoatColor FROM Cat ORDER BY Id");

// Once the first value is accessed, the entire batch gets executed
Cat cat1 = await val1.GetValueAsync(); // Prefer async for the first call but sync version works too
Cat? cat2 = val2.GetValue();
Cat cat3 = val3.GetValue();
Cat? cat4 = val4.GetValue();

Console.WriteLine($"Cat 1 QueryFirstBatched: {cat1}"); // Cat 1 QueryFirstBatched: |1: Garfield (Orange)|
Console.WriteLine($"Cat 2 QueryFirstOrDefaultBatched: {cat2}"); // Cat 2 QueryFirstOrDefaultBatched: |2: Tom (Gray)|
Console.WriteLine($"Cat 3 QuerySingleBatched: {cat3}"); // Cat 3 QuerySingleBatched: |3: Sylvester (Tuxedo)|
Console.WriteLine($"Cat 4 QuerySingleOrDefaultBatched: {cat4}"); // Cat 4 QuerySingleOrDefaultBatched: 
Console.WriteLine($"Inserted {affectedRows1.GetValue()} ({affectedRows2.GetValue()}) cats"); // Inserted 2 (2) cats
Console.WriteLine($"Now there're {allCats.GetValue().Count()} cats in total"); // Now there're 7 cats in total

API

There're a few extension methods named similarly to Dapper but they end with the prefix Batched:

  • IBatchedValue<IEnumerable<T>> QueryBatched<T>(...)
  • IBatchedValue<T> QueryFirstBatched<T>(...)
  • IBatchedValue<T?> QueryFirstOrDefaultBatched<T>(...)
  • IBatchedValue<T> QuerySingleBatched<T>(...)
  • IBatchedValue<T?> QuerySingleOrDefaultBatched<T>(...)
  • IBatchedValue<int> ExecuteBatched(...)

Each method returns an interface IBatchedValue<T> which provides two functions: GetValueAsync and GetValue. When the first value is retrieved, the entire batch gets sent to the database as a single command. After that you may obtain the values repeatedly - they are cached and returned synchronously.

Important note about managing connections

All related *Batched calls must be made on the same connection object because the batch metadata is stored via ConditionalWeakTable. This means that the calls made for different connections are batched separately, providing better isolation and error-handling. If you're injecting your IDbConnection via IServiceProvider, then consider Scoped lifetime instead of Transient.

License

MIT but be sure to check license for Dapper separately.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • Dapper (>= 2.1.66 && < 3.0.0)

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
1.0.1 269 8/25/2025
1.0.0 270 8/25/2025