This commit is contained in:
Janus C. H. Knudsen 2025-03-10 15:56:22 +01:00
parent f3ab94eff1
commit 31666b4ba0
34 changed files with 140 additions and 83 deletions

View file

@ -0,0 +1,8 @@
namespace PlanTempus.Core.Database.ConnectionFactory
{
public interface IDbConnectionFactory
{
System.Data.IDbConnection Create();
System.Data.IDbConnection Create(ConnectionStringParameters connectionStringTemplateParameters);
}
}

View file

@ -0,0 +1,67 @@
using System.Data;
using Npgsql;
namespace PlanTempus.Core.Database.ConnectionFactory
{
public record ConnectionStringParameters(string User, string Pwd);
public class PostgresConnectionFactory : IDbConnectionFactory, IAsyncDisposable
{
private readonly NpgsqlDataSource _baseDataSource;
private readonly Action<NpgsqlDataSourceBuilder> _configureDataSource;
private readonly Microsoft.Extensions.Logging.ILoggerFactory _loggerFactory; //this is not tested nor implemented, I just created it as an idea
public PostgresConnectionFactory(
string connectionString,
Microsoft.Extensions.Logging.ILoggerFactory loggerFactory = null,
Action<NpgsqlDataSourceBuilder> configureDataSource = null)
{
_loggerFactory = loggerFactory;
_configureDataSource = configureDataSource ?? (builder => { });
// Opret base data source med konfiguration
var dataSourceBuilder = new NpgsqlDataSourceBuilder(connectionString);
ConfigureDataSourceBuilder(dataSourceBuilder);
_baseDataSource = dataSourceBuilder.Build();
}
public IDbConnection Create()
{
return _baseDataSource.CreateConnection();
}
public IDbConnection Create(ConnectionStringParameters param)
{
var connectionStringBuilder = new NpgsqlConnectionStringBuilder(
_baseDataSource.ConnectionString)
{
Username = param.User,
Password = param.Pwd
};
var tempDataSourceBuilder = new NpgsqlDataSourceBuilder(
connectionStringBuilder.ToString());
ConfigureDataSourceBuilder(tempDataSourceBuilder);
var tempDataSource = tempDataSourceBuilder.Build();
return tempDataSource.CreateConnection();
}
private void ConfigureDataSourceBuilder(NpgsqlDataSourceBuilder builder)
{
if (_loggerFactory != null)
{
builder.UseLoggerFactory(_loggerFactory);
}
_configureDataSource?.Invoke(builder);
}
public async ValueTask DisposeAsync()
{
await _baseDataSource.DisposeAsync();
}
}
}

View file

@ -0,0 +1,31 @@
using System.Data;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
namespace PlanTempus.Core.Database;
public class DatabaseScope : IDisposable
{
private readonly IOperationHolder<DependencyTelemetry> _operation;
public DatabaseScope(IDbConnection connection, IOperationHolder<DependencyTelemetry> operation)
{
Connection = connection;
_operation = operation;
_operation.Telemetry.Success = true;
}
public IDbConnection Connection { get; }
public void Dispose()
{
_operation.Dispose();
Connection.Dispose();
}
public void Error(Exception ex)
{
_operation.Telemetry.Success = false;
_operation.Telemetry.Properties["Error"] = ex.Message;
}
}

View file

@ -0,0 +1,10 @@
using System.Data;
namespace PlanTempus.Core.Database;
public interface IDatabaseOperations
{
DatabaseScope CreateScope(string operationName);
Task<T> ExecuteAsync<T>(Func<IDbConnection, Task<T>> operation, string operationName);
Task ExecuteAsync(Func<IDbConnection, Task> operation, string operationName);
}

View file

@ -0,0 +1,57 @@
using System.Data;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using PlanTempus.Core.Database.ConnectionFactory;
namespace PlanTempus.Core.Database;
public class SqlOperations : IDatabaseOperations
{
private readonly IDbConnectionFactory _connectionFactory;
private readonly TelemetryClient _telemetryClient;
public SqlOperations(IDbConnectionFactory connectionFactory, TelemetryClient telemetryClient)
{
_connectionFactory = connectionFactory;
_telemetryClient = telemetryClient;
}
public DatabaseScope CreateScope(string operationName)
{
var connection = _connectionFactory.Create();
var operation = _telemetryClient.StartOperation<DependencyTelemetry>(operationName);
operation.Telemetry.Type = "SQL";
operation.Telemetry.Target = "PostgreSQL";
return new DatabaseScope(connection, operation);
}
public async Task<T> ExecuteAsync<T>(Func<IDbConnection, Task<T>> operation, string operationName)
{
using var scope = CreateScope(operationName);
try
{
var result = await operation(scope.Connection);
return result;
}
catch (Exception ex)
{
scope.Error(ex);
throw;
}
}
public async Task ExecuteAsync(Func<IDbConnection, Task> operation, string operationName)
{
using var scope = CreateScope(operationName);
try
{
await operation(scope.Connection);
}
catch (Exception ex)
{
scope.Error(ex);
throw;
}
}
}