Faactory.DbContext.Npgsql
0.4.0
See the version list below for details.
dotnet add package Faactory.DbContext.Npgsql --version 0.4.0
NuGet\Install-Package Faactory.DbContext.Npgsql -Version 0.4.0
<PackageReference Include="Faactory.DbContext.Npgsql" Version="0.4.0" />
paket add Faactory.DbContext.Npgsql --version 0.4.0
#r "nuget: Faactory.DbContext.Npgsql, 0.4.0"
// Install Faactory.DbContext.Npgsql as a Cake Addin #addin nuget:?package=Faactory.DbContext.Npgsql&version=0.4.0 // Install Faactory.DbContext.Npgsql as a Cake Tool #tool nuget:?package=Faactory.DbContext.Npgsql&version=0.4.0
ADO.NET Extensions
This project contains a set of extensions to help with managing multiple data sources, more specifically, inside a DI scenario.
Getting started
Before we can use the extensions, we need to register the context provider with our DI container. We'll have to install the appropriate package; the following providers are currently supported
Provider | Package |
---|---|
SqlServer | Faactory.DbContext.SqlClient |
PostgreSql | Faactory.DbContext.Npgsql |
We'll use SqlServer as an example
$ dotnet add package Faactory.DbContext.SqlClient
On our Startup.cs
class, we'll register the provider and configure our databases; we can add multiple contexts.
IServiceCollection services = ...
services.AddSqlDbContextProvider()
.AddDbContext( "my-db", "connection_string" )
.AddDbContext( "my-other-db", "connection_string" );
Wherever we need to get access to our database context, we'll use the injected IDbContextFactory
instance to retrieve an IDbContext
instance.
public class Example
{
private readonly IDbContext mydb;
public Example( IDbContextFactory dbContextFactory )
{
mydb = dbContextFactory.GetDbContext( "my-db" );
}
...
}
When we require a new connection, we'll use the IDbContext
instance to construct a new connection.
public class Example
{
private readonly IDbContext mydb;
...
public async Task DoSomethingAsync()
{
using ( var connection = mydb.GetDbConnection() )
{
await connection.OpenAsync();
...
}
}
}
We can also construct the connection and open it directly
public async Task DoSomethingAsync()
{
using ( var connection = await mydb.OpenAsync() )
{
...
}
}
From this point forward, we'll have an IDbConnection
instance ready to use.
Note: All
IDbConnection
instances should be properly disposed after use. Most of the ADO implementations will pool connections and not properly disposing the connections can lead to exceeding the number of open connections.
Transactions
As an alternative to the IDbConnection.BeginTransaction
method, there are extensions available to shorten the syntax. The UseTransaction[Async]
methods take care of opening/reusing a connection, creating a transaction and gracefully disposing of it all when finished.
public async Task DoSomethingAsync()
{
await mydb.UseTransactionAsync( async t =>
{
var sqlCommand = t.Connection.CreateCommand();
...
await t.CommitAsync();
} );
}
If an error occurs, the transaction is automatically rolled back. We can also provide additional behaviour to when this happens.
public async Task DoSomethingAsync()
{
await mydb.UseTransactionAsync( async t =>
{
var sqlCommand = t.Connection.CreateCommand();
...
await t.CommitAsync();
}, ex =>
{
// handle exception
} );
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | 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 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. |
-
net6.0
- Faactory.DbContext.Core (>= 0.4.0)
- Microsoft.Extensions.Options (>= 6.0.0)
- Npgsql (>= 6.0.6)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
0.7.2 | 84 | 8/27/2024 |
0.7.1 | 72 | 8/27/2024 |
0.7.0 | 99 | 5/31/2024 |
0.6.1 | 101 | 3/8/2024 |
0.6.0 | 242 | 10/23/2023 |
0.6.0-preview-1 | 83 | 9/27/2023 |
0.5.2-preview-2 | 102 | 9/26/2023 |
0.5.2-preview-1 | 94 | 9/25/2023 |
0.5.1 | 171 | 7/7/2023 |
0.5.0 | 159 | 4/27/2023 |
0.4.0 | 387 | 8/9/2022 |
0.3.5 | 416 | 3/23/2022 |
0.3.4 | 395 | 3/18/2022 |
0.3.3 | 390 | 3/18/2022 |
0.3.2 | 393 | 3/18/2022 |
0.3.1 | 395 | 3/18/2022 |
0.3.0 | 414 | 3/17/2022 |
0.2.2 | 415 | 3/16/2022 |
0.2.1 | 306 | 10/4/2021 |
0.1.0 | 311 | 9/29/2021 |