SWPCore/Core/Database/SqlOperations.cs
Janus C. H. Knudsen 3aeae6315b Improves configuration and telemetry handling in tests
Adds null checks for telemetry and logging configurations
Prevents registration of modules with empty configuration
Updates test configuration file copy behavior

Enhances robustness of test infrastructure by conditionally registering telemetry and logging modules only when valid configuration is present
2025-08-03 14:10:49 +02:00

57 lines
1.6 KiB
C#

using System.Data;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using SWP.Core.Database.ConnectionFactory;
namespace SWP.Core.Database;
public class SqlOperations : IDatabaseOperations
{
private readonly IDbConnectionFactory _connectionFactory;
private readonly TelemetryClient _telemetryClient;
public SqlOperations(IDbConnectionFactory connectionFactory, TelemetryClient telemetryClient = null)
{
_connectionFactory = connectionFactory;
_telemetryClient = telemetryClient ?? new 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;
}
}
}