-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
212 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<Project> | ||
|
||
<PropertyGroup> | ||
<AccelerateBuildsInVisualStudio>true</AccelerateBuildsInVisualStudio> | ||
<NoWarn>$(NoWarn);CS8604</NoWarn> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project> | ||
<PropertyGroup> | ||
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally> | ||
<CentralPackageVersionOverrideEnabled>false</CentralPackageVersionOverrideEnabled> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" /> | ||
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" /> | ||
<PackageVersion Include="Dapper" Version="2.0.143" /> | ||
<PackageVersion Include="Npgsql" Version="8.0.1" /> | ||
<!-- Test references --> | ||
<PackageVersion Include="xunit" Version="2.5.0" /> | ||
<PackageVersion Include="xunit.assert" Version="2.4.2" /> | ||
<PackageVersion Include="xunit.runner.visualstudio" Version="2.4.5" /> | ||
<PackageVersion Include="xunit.extensibility.core" Version="2.4.2" /> | ||
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.6.0" /> | ||
<PackageVersion Include="coverlet.collector" Version="6.0.0" /> | ||
</ItemGroup> | ||
</Project> |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System.Data.Common; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Npgsql; | ||
using TransactionContext.Configurators; | ||
using TransactionContext.Postgres.Internals; | ||
|
||
namespace TransactionContext.Postgres | ||
{ | ||
public static class Extensions | ||
{ | ||
public static void UsePostgres( | ||
this IDbProviderConfigurator configurator, | ||
string connectionString) | ||
{ | ||
configurator.Services.AddSingleton<IDbConnectionFactory>(new PostgresConnectionFactory(connectionString)); | ||
configurator.Services.AddScoped<DbBatch, NpgsqlBatch>(); | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
source/TransactionContext.Postgres/Internals/PostgresConnectionFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Npgsql; | ||
using System.Data.Common; | ||
|
||
namespace TransactionContext.Postgres.Internals | ||
{ | ||
internal sealed class PostgresConnectionFactory : IDbConnectionFactory | ||
{ | ||
private readonly string _connectionString; | ||
|
||
public PostgresConnectionFactory(string connectionString) => _connectionString = connectionString; | ||
|
||
public DbConnection Create() => new NpgsqlConnection(_connectionString); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
source/TransactionContext/Configurators/IDbProviderConfigurator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace TransactionContext.Configurators | ||
{ | ||
public interface IDbProviderConfigurator | ||
{ | ||
IServiceCollection Services { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using TransactionContext.Configurators; | ||
using TransactionContext.Internals; | ||
|
||
namespace TransactionContext | ||
{ | ||
public static class Extensions | ||
{ | ||
public static void AddTransactionContext( | ||
this IServiceCollection services, | ||
Action<IDbProviderConfigurator> configure) | ||
{ | ||
var configuratior = new DbProviderConfigurator(services); | ||
|
||
configure(configuratior); | ||
|
||
services.AddScoped<ITransactionContext, InternalTransactionContext>(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Data.Common; | ||
|
||
namespace TransactionContext | ||
{ | ||
public interface IDbConnectionFactory | ||
{ | ||
DbConnection Create(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System.Data; | ||
|
||
namespace TransactionContext | ||
{ | ||
public interface ITransactionContext | ||
{ | ||
void Add(string sql, object? parameters = null, CommandType command = CommandType.Text); | ||
|
||
Task Commit(CancellationToken cancellationToken = default); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
source/TransactionContext/Internals/DbProviderConfigurator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using TransactionContext.Configurators; | ||
|
||
namespace TransactionContext.Internals | ||
{ | ||
internal sealed class DbProviderConfigurator : IDbProviderConfigurator | ||
{ | ||
public IServiceCollection Services { get; private set; } | ||
|
||
internal DbProviderConfigurator(IServiceCollection services) => Services = services; | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
source/TransactionContext/Internals/InternalTransactionContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using System.Data.Common; | ||
using System.Data; | ||
|
||
namespace TransactionContext.Internals | ||
{ | ||
internal sealed class InternalTransactionContext : ITransactionContext | ||
{ | ||
private readonly IDbConnectionFactory _connectionFactory; | ||
|
||
private readonly DbBatch _dbBatch; | ||
|
||
public InternalTransactionContext( | ||
IDbConnectionFactory connectionFactory, | ||
DbBatch dbBatch) | ||
{ | ||
_connectionFactory = connectionFactory; | ||
_dbBatch = dbBatch; | ||
} | ||
|
||
public void Add(string sql, object? parameters = null, CommandType command = CommandType.Text) | ||
{ | ||
var cmd = _dbBatch.CreateBatchCommand(); | ||
cmd.CommandText = sql; | ||
cmd.CommandType = command; | ||
|
||
if (parameters != null) | ||
{ | ||
var properties = parameters.GetType().GetProperties(); | ||
|
||
foreach (var property in properties) | ||
{ | ||
var parameter = cmd.CreateParameter(); | ||
|
||
parameter.ParameterName = property.Name; | ||
parameter.Value = property.GetValue(parameters); | ||
|
||
cmd.Parameters.Add(parameter); | ||
} | ||
} | ||
|
||
_dbBatch.BatchCommands.Add(cmd); | ||
} | ||
|
||
public async Task Commit(CancellationToken cancellationToken = default) | ||
{ | ||
using (DbConnection connection = _connectionFactory.Create()) | ||
{ | ||
await connection | ||
.OpenAsync(cancellationToken) | ||
.ConfigureAwait(false); | ||
|
||
using (DbTransaction transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted)) | ||
{ | ||
try | ||
{ | ||
_dbBatch.Connection = connection; | ||
_dbBatch.Transaction = transaction; | ||
|
||
await _dbBatch | ||
.ExecuteNonQueryAsync(cancellationToken) | ||
.ConfigureAwait(false); | ||
|
||
await transaction | ||
.CommitAsync(cancellationToken) | ||
.ConfigureAwait(false); | ||
} | ||
catch (Exception) | ||
{ | ||
await transaction | ||
.RollbackAsync(cancellationToken) | ||
.ConfigureAwait(false); | ||
|
||
throw; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters