Getting Started with EricksonLopez.DapperExtensions
L3StableNuGet: v2.0.0Local HEAD: v3.1.0AOT: Full
Install EricksonLopez.DapperExtensions and the target dialect/DI packages (2.0.0 on NuGet).
dotnet add package EricksonLopez.DapperExtensions --version 2.0.0<PackageReference Include="EricksonLopez.DapperExtensions" Version="2.0.0" /><PackageVersion Include="EricksonLopez.DapperExtensions" Version="2.0.0" />dotnet add package EricksonLopez.DapperExtensions.DependencyInjection --version 2.0.0dotnet add package EricksonLopez.DapperExtensions.PostgreSql --version 2.0.0dotnet add package EricksonLopez.DapperExtensions.SourceGenerators --version 2.0.0Publication Note:
EricksonLopez.DapperExtensions.Analyzersexists only in local3.1.0HEAD and is not published on NuGet.org.
Quick Start: Validated DI & WithUnitOfWorkAsync
Section titled “Quick Start: Validated DI & WithUnitOfWorkAsync”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); }}