Skip to content

Search EricksonLopez.* Ecosystem

Type a library name, NuGet package ID, .NET type, or architectural concept to search across the portal.

Getting Started with EricksonLopez.DapperExtensions

Install EricksonLopez.DapperExtensions and the target dialect/DI packages (2.0.0 on NuGet).

dotnet add package EricksonLopez.DapperExtensions --version 2.0.0
Terminal window
dotnet add package EricksonLopez.DapperExtensions.DependencyInjection --version 2.0.0
dotnet add package EricksonLopez.DapperExtensions.PostgreSql --version 2.0.0
dotnet add package EricksonLopez.DapperExtensions.SourceGenerators --version 2.0.0

Publication Note: EricksonLopez.DapperExtensions.Analyzers exists only in local 3.1.0 HEAD and is not published on NuGet.org.

Register standard type handlers, transient error detectors, and keyed resilience pipelines, then execute Dapper commands inside WithUnitOfWorkAsync:

using System;
using System.Data;
using System.Threading;
using System.Threading.Tasks;
using Dapper;
using EricksonLopez.DapperExtensions;
using EricksonLopez.DapperExtensions.DependencyInjection;
using EricksonLopez.DapperExtensions.UnitOfWork;
using EricksonLopez.Resilience;
using Microsoft.Extensions.DependencyInjection;
using Npgsql;
namespace Example.DapperExtensionsQuickStart;
[SqlEntity(TableName = "orders")]
public sealed partial class OrderEntity
{
public long Id { get; set; }
public string Status { get; set; } = string.Empty;
public decimal Total { get; set; }
}
public static class DapperExtensionsUsage
{
public static void Register()
{
var services = new ServiceCollection();
services.AddDapperExtensions(options =>
{
options.RegisterStandardTypeHandlers = true;
options.RegisterTransientErrorDetectors = true;
});
services.AddDapperResiliencePipelines();
using var provider = services.BuildServiceProvider(
new ServiceProviderOptions { ValidateOnBuild = true, ValidateScopes = true });
var pgPipeline = provider.GetRequiredKeyedService<IResiliencePipeline>("postgresql");
if (pgPipeline is null)
{
throw new InvalidOperationException("PostgreSQL resilience pipeline was not registered.");
}
}
public static async Task PersistOrderAsync(
NpgsqlConnection connection,
CancellationToken ct)
{
await connection.WithUnitOfWorkAsync(
async (uow, token) =>
{
var cmd = new CommandDefinition(
"UPDATE orders SET status = @Status WHERE id = @Id",
new { Id = 1001L, Status = "Confirmed" },
transaction: uow.Transaction,
cancellationToken: token);
await connection.ExecuteAsync(cmd);
},
IsolationLevel.ReadCommitted,
ct);
}
}