IntraDotNet.DapperExtensions
1.0.2
dotnet add package IntraDotNet.DapperExtensions --version 1.0.2
NuGet\Install-Package IntraDotNet.DapperExtensions -Version 1.0.2
<PackageReference Include="IntraDotNet.DapperExtensions" Version="1.0.2" />
<PackageVersion Include="IntraDotNet.DapperExtensions" Version="1.0.2" />
<PackageReference Include="IntraDotNet.DapperExtensions" />
paket add IntraDotNet.DapperExtensions --version 1.0.2
#r "nuget: IntraDotNet.DapperExtensions, 1.0.2"
#:package IntraDotNet.DapperExtensions@1.0.2
#addin nuget:?package=IntraDotNet.DapperExtensions&version=1.0.2
#tool nuget:?package=IntraDotNet.DapperExtensions&version=1.0.2
IntraDotNet.DapperExtensions
Utility library for Dapper to improve the development experience of common Dapper scenarios.
Initializing a Dependency Injectable Context Wrapper for Dapper
First you need to create a context class that inherits from DapperContext
, then implement the GetDbConnection
function.
The following example uses a connection to an Sql Server database which requires installing either System.Data.SqlClient
or Microsoft.Data.SqlClient
depending on your target runtime, however you can use any database provider by installing the package relevant for that provider, if the provider implements the .NET Data Provider schema. .NET Data Providers
using System.Data.Common;
using System.Data.SqlClient;
//Include the DapperExtensions namespace
using IntraDotNet.DapperExtensions.Context;
namespace TestApp;
/// <summary>
/// This class is used to create a local SQL Server Dapper context.
/// It inherits from the DapperContext class and overrides the GetDbConnection method.
/// The GetDbConnection method creates a new SqlConnection object using the connection string
/// provided in the DapperContextOptions.
/// </summary>
public class LocalSqlDapperContext : DapperContext
{
public LocalSqlDapperContext() : base()
{
}
public LocalSqlDapperContext(DapperContextOptions options) : base(options)
{
}
public override DbConnection GetDbConnection()
{
#pragma warning disable CS0618 // Type or member is obsolete
SqlConnection connection = new SqlConnection(Options.ConnectionString);
#pragma warning restore CS0618 // Type or member is obsolete
return connection;
}
}
Registering the Dapper Context in Dependency Injection
You can register your Dapper context in two ways:
1. Using a configuration action (like EF Core):
internal class Program
{
private static void Main(string[] args)
{
HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
builder.Configuration.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
builder.Services.AddDapperContext<LocalSqlDapperContext>(options =>
{
options.ConnectionString = builder.Configuration.GetConnectionString("LocalSql");
});
builder.Services.AddHostedService<Worker>();
IHost host = builder.Build();
host.Run();
}
}
2. Using appsettings.json and IOptions pattern:
Add your connection string to appsettings.json
:
{
"ConnectionStrings": {
"LocalSql": "your_connection_string"
},
"DapperContextOptions": {
"ConnectionString": "your_connection_string"
}
}
Then register the context using the IOptions pattern:
internal class Program
{
private static void Main(string[] args)
{
HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
builder.Configuration.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
// Bind DapperContextOptions from configuration
builder.Services.Configure<DapperContextOptions>(builder.Configuration.GetSection("DapperContextOptions"));
// Register context using IOptions
builder.Services.AddDapperContext<ILocalSqlDapperContext, LocalSqlDapperContext>();
builder.Services.AddHostedService<Worker>();
IHost host = builder.Build();
host.Run();
}
}
Injecting and Using the Dapper Context
Then dependency inject the DapperContext at the point it is required in your code:
using System.Data.Common;
using Microsoft.Extensions.Hosting;
using Dapper;
namespace TestApp;
public sealed class Worker : IHostedService, IHostedLifecycleService
{
private readonly LocalSqlDapperContext _context;
public Worker(LocalSqlDapperContext context)
{
//Dependency injected here
_context = context;
}
public Task StartAsync(CancellationToken cancellationToken)
{
Console.WriteLine("Worker started.");
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
Console.WriteLine("Worker stopped.");
return Task.CompletedTask;
}
public void OnStarting()
{
Console.WriteLine("OnStarting called.");
}
public void OnStopping()
{
Console.WriteLine("OnStopping called.");
}
public Task StartingAsync(CancellationToken cancellationToken)
{
Console.WriteLine("Worker starting.");
return Task.CompletedTask;
}
public Task StartedAsync(CancellationToken cancellationToken)
{
Console.WriteLine("Worker started.");
// Query executed here
using DbConnection connection = _context.GetDbConnection();
var id = connection.Query("SELECT 1 AS Id").FirstOrDefault();
return Task.CompletedTask;
}
public Task StoppingAsync(CancellationToken cancellationToken)
{
Console.WriteLine("Worker stopping.");
return Task.CompletedTask;
}
public Task StoppedAsync(CancellationToken cancellationToken)
{
Console.WriteLine("Worker stopped.");
return Task.CompletedTask;
}
}
IMPORTANT: Remember the DbConnection
is an unmanaged resource and must be wrapped in a using
to prevent memory leakage.
Contributing
Contributions are welcome! Please open an issue or submit a pull request.
License
This project is licensed under the MIT License.
Product | Versions 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 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. |
-
.NETStandard 2.0
- Microsoft.Extensions.Configuration (>= 9.0.5)
- Microsoft.Extensions.DependencyInjection (>= 9.0.5)
- Microsoft.Extensions.Options (>= 9.0.5)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.