Skip to content

Commit

Permalink
Added MVP-1
Browse files Browse the repository at this point in the history
  • Loading branch information
adimiko committed Feb 5, 2024
1 parent 7adb2c9 commit 6017dab
Show file tree
Hide file tree
Showing 14 changed files with 212 additions and 12 deletions.
8 changes: 8 additions & 0 deletions Directory.Build.props
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>
19 changes: 19 additions & 0 deletions Directory.Packages.props
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>
6 changes: 0 additions & 6 deletions source/TransactionContext.Postgres/Class1.cs

This file was deleted.

19 changes: 19 additions & 0 deletions source/TransactionContext.Postgres/Extensions.cs
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>();
}
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,12 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Npgsql" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\TransactionContext\TransactionContext.csproj" />
</ItemGroup>

</Project>
6 changes: 0 additions & 6 deletions source/TransactionContext/Class1.cs

This file was deleted.

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; }
}
}
20 changes: 20 additions & 0 deletions source/TransactionContext/Extensions.cs
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>();
}
}
}
9 changes: 9 additions & 0 deletions source/TransactionContext/IDbConnectionFactory.cs
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();
}
}
11 changes: 11 additions & 0 deletions source/TransactionContext/ITransactionContext.cs
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 source/TransactionContext/Internals/DbProviderConfigurator.cs
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 source/TransactionContext/Internals/InternalTransactionContext.cs
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;
}
}
}
}
}
}
4 changes: 4 additions & 0 deletions source/TransactionContext/TransactionContext.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" />
</ItemGroup>

</Project>

0 comments on commit 6017dab

Please sign in to comment.