Dapper.Abstractions 4.0.0

dotnet add package Dapper.Abstractions --version 4.0.0                
NuGet\Install-Package Dapper.Abstractions -Version 4.0.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.Abstractions" Version="4.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Dapper.Abstractions --version 4.0.0                
#r "nuget: Dapper.Abstractions, 4.0.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.
// Install Dapper.Abstractions as a Cake Addin
#addin nuget:?package=Dapper.Abstractions&version=4.0.0

// Install Dapper.Abstractions as a Cake Tool
#tool nuget:?package=Dapper.Abstractions&version=4.0.0                

Dapper.Abstractions

Support for .NET Standard 2.0, .NET 6.0 and .NET 8.0

Breaking Change Notice

We have introduced a breaking change in the release 4.x to improve the modularity and flexibility of our codebase. As part of this update, we have separated the SQL Client from Dapper.Abstractions into its own package, SqlDapper.Abstractions.

To use then in code add an additional using statement

using Dapper.Abstractions.Sql;

NOTE:

The Dapper library has a Preserved Prefix on the Dapper.* package naming on nuget.org and therefore the new packages for splitting out the SQL Client required new naming in order to not conflict with this.

Impact:
  • Users will need to update their code to use the new SqlDapper.Abstractions package instead of the Dapper.Abstractions package to continue utilizing the SQL Client functionality that was previously part of Dapper.Abstractions.

Introduction

Dapper.Abstractions is a fork of DapperWrapper and is a library that wraps the Dapper extension methods on IDbConnection to make unit testing easier.

Why bother? Because stubbing the extension methods used in a method-under-unit-test is not simple. For instance, you can't just use a library like Moq or NSubstitute to stub the .Query extension method on a fake IDbConnection. To work around this, this library introduces a new abstraction, IDbExecutor.

The IDbExecutor Interface

The IDbExectuor interface has many methods, each corresponding to a Dapper extension method: Execute, Query, Query<T>, QueryMultiple, QueryMultiple<T>, etc.. Wherever you would previously inject an IDbConnection to use with Dapper, you instead inject an IDbExecutor. There is a single implementation of IDbExecutor included in Dapper.Abstractions, SqlExecutor, that uses the Dapper extension methods against SqlConnection. Adding your own IDbExecutor against other implementations of IDbConnection is easy.

Example use of IDbExecutor:

public IEnumerable<SemanticVersion> GetAllPackageVersions(string packageId, IDbExecutor dbExecutor)
{
  return dbExecutor.Query<string>("SELECT p.version FROM packages p WHERE p.id = @packageId", new { packageId })
    .Select(version => new SemanticVersion(version));
}

Example use of IDbExecutorFactory:

  private IDbExecutorFactory _dbExecutorFactory;
  public UserAccess(IDbExecutorFactory dbExecutorFactory)
  {
      _dbExecutorFactory = dbExecutorFactory
  }

  public IEnumerable<User> GetUsers()
  {
      using var db = _dbExecutorFactory.CreateExecutor();
      var data = db.Query<User>("SELECT ID, Name FROM Users");
      return data;
  }

Injecting IDbExecutor

You probably already have an approach to injecting IDbConnection into your app that you're happy with. That same approach will probably work just as well with IDbExecutor or IDbExecutorFactory.

Example ASP.NET .NET 6

In Program.cs

using Dapper.Abstractions;
using Dapper.Abstractions.Sql;

var builder = WebApplication.CreateBuilder(args);
...
var connectionString = builder.Configuration["database:connectionstring"];

var dbExecutorFactory = new SqlExecutorFactory(connectionString);

builder.Services.AddSingleton<IDbExecutorFactory>(dbExecutorFactory);
...

Example .NET 6 Console

using Dapper.Abstractions;
using Dapper.Abstractions.Sql;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

using IHost host = Host.CreateDefaultBuilder(args)
    .ConfigureServices((builder, services) =>
        services.AddSingleton<IDbExecutorFactory>(new SqlExecutorFactory(builder.Configuration["DatabaseConnectionString"])))
    .Build();

or

using Dapper.Abstractions;
using Dapper.Abstractions.Sql;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

using IHost host = Host.CreateDefaultBuilder(args)
    .ConfigureServices((builder, services) =>
        services.AddSingleton<IDbExecutor>(new SqlExecutor(new SqlConnection(builder.Configuration["DatabaseConnectionString"]))))
    .Build();

Additional Extensions

There are also times when the data coming from the database is not trimmed and so Dapper.Abstractions includes QueryAndTrimResults<T> for this purpose.

Example usage

  private IDbExecutorFactory _dbExecutorFactory;
  public UserAccess(IDbExecutorFactory dbExecutorFactory)
  {
      _dbExecutorFactory = dbExecutorFactory
  }

  public IEnumerable<User> GetUsers()
  {
      using var db = _dbExecutorFactory.CreateExecutor();
      var data = db.QueryAndTrimResults<User>("SELECT ID, Name FROM Users");
      return data;
  }

Transactions

Sometimes there is a need to assert whether a method-under-unit-test completes a transaction via TransactionScope. To make this easier, Dapper.Abstractions also has an ITransactionScope interface (and TransactionScopeAbstraction implementation) that makes it easy to create a fake transaction, and stub (and assert on) the Complete method. As with IDbExecutor, you can bind it directly, via Func<ITransactionScope>.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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 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. 
.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 was computed.  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 (2)

Showing the top 2 NuGet packages that depend on Dapper.Abstractions:

Package Downloads
SqlDapper.Abstractions

A simple abstraction on top of the Dapper extension synchronous and asynchronous methods to aid testability with SQL Client

OracleDapper.Abstractions

A simple abstraction on top of the Dapper extension synchronous and asynchronous methods to aid testability with Oracle client.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
4.0.0 1,253 6/30/2024
3.0.0 10,011 6/11/2022
2.0.1 51,462 4/30/2018
2.0.0 1,531 4/29/2018
1.0.3 5,584 10/18/2016
1.0.2 1,949 9/23/2016
1.0.1 2,913 2/24/2016

** Breaking change ** Separation of the abstractions to remove dependency on SQL Client now supported by Dapper.Abstractions.Sql. .NET 8 support added as well as package updates to address security vulnerabilities