Dapper.Extensions.Microsoft.DependencyInjection
1.0.1
dotnet add package Dapper.Extensions.Microsoft.DependencyInjection --version 1.0.1
NuGet\Install-Package Dapper.Extensions.Microsoft.DependencyInjection -Version 1.0.1
<PackageReference Include="Dapper.Extensions.Microsoft.DependencyInjection" Version="1.0.1" />
<PackageVersion Include="Dapper.Extensions.Microsoft.DependencyInjection" Version="1.0.1" />
<PackageReference Include="Dapper.Extensions.Microsoft.DependencyInjection" />
paket add Dapper.Extensions.Microsoft.DependencyInjection --version 1.0.1
#r "nuget: Dapper.Extensions.Microsoft.DependencyInjection, 1.0.1"
#:package Dapper.Extensions.Microsoft.DependencyInjection@1.0.1
#addin nuget:?package=Dapper.Extensions.Microsoft.DependencyInjection&version=1.0.1
#tool nuget:?package=Dapper.Extensions.Microsoft.DependencyInjection&version=1.0.1
How to register DependencyInjection
Dapper.Extensions.Microsoft.DependencyInjection was created to achieve the same ease as EntityFramework's DbContext. Using DependencyInjection we must add our DapperContext as shown in the Program.cs example.
The use of UseSqlServerUserSecrets serves to help in the configuration of the ConnectionString. By default it will search in the appsettings.json file in the ConnectionStrings section DefaultConnection property, which can be changed when configuring dependency injection.
How to change the property DefaultConnection of the DapperContext
services.AddDapperDbContext<MyDapperContext>(p => p.UseSqlServerUserSecrets(hostContext.Configuration, "MyDefaultConnection"), ServiceLifetime.Singleton);
How the ConnectionString password is retrieved.
The DependencyInjection looks for the configuration of appsettings, concatenating the name of the ConnectionString + Password, by default DefaultConnectionPassword. Case another name such especify, as MyDefaultConnection, it will look for the configuration key in property "MyDefaultConnectionPassword"
How to use DapperContext
Below is an example from MyRepository:
public class MyRepository
{
private MyDapperContext _myDapperContext;
public MyRepository(MyDapperContext myDapperContext)
{
_myDapperContext = myDapperContext;
}
public IEnumerable<Person> GetAll()
{
return _myDapperContext.Person.GetAll();
}
}
#Below is an example from Program:
Program.cs
public static class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
private static IHostBuilder CreateHostBuilder(string[] args)
{
return Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.AddDapperDbContext<MyDapperContext>(p => p.UseSqlServerUserSecrets(hostContext.Configuration), ServiceLifetime.Singleton);
services.AddHostedService<Worker>();
});
}
}
MyDapperContext.cs
public class MyDapperContext : DapperContext
{
public DapperDbSet<Person> Person { get; set; }
public MyDapperContext(DapperContextOptions<MyDapperContext> pDapperContextOptions) : base(pDapperContextOptions)
{
}
}
Person.cs
[Table("Persons")]
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
appsettings.json
{
"ConnectionStrings": {
"DefaultConnection": "Server=localhost;Database=MyDapperDataBase;User Id=User;"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
secrets.json
{
"DefaultConnectionPassword": "l^#!hODH~19UR4T"
}
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 is compatible. |
.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. |
-
.NETCoreApp 3.1
- Dapper.Contrib (>= 2.0.30)
- JetBrains.Annotations (>= 2019.1.3)
- Microsoft.Extensions.Configuration (>= 3.1.2)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 3.1.2)
- System.Data.SqlClient (>= 4.8.1)
-
.NETStandard 2.0
- Dapper.Contrib (>= 2.0.30)
- JetBrains.Annotations (>= 2019.1.3)
- Microsoft.Extensions.Configuration (>= 3.1.2)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 3.1.2)
- System.Data.SqlClient (>= 4.8.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.