c59a3160d0
Add CreateGiwConnectionAsync to IDbConnectionFactory and DbConnectionFactory for connecting to the GIW Oracle database. This connection is needed for the StatusCode data sync pipeline. - Reuses existing CreateOracleConnectionAsync helper with "GIW" data source - Follows same pattern as JDE, JDEStage, and CMS connections - Includes 4 unit tests covering missing/empty/invalid connection strings
137 lines
4.8 KiB
C#
137 lines
4.8 KiB
C#
using JdeScoping.DataAccess.Exceptions;
|
|
using JdeScoping.DataAccess.Interfaces;
|
|
using Microsoft.Data.SqlClient;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Logging;
|
|
using Oracle.ManagedDataAccess.Client;
|
|
|
|
namespace JdeScoping.DataAccess;
|
|
|
|
/// <summary>
|
|
/// Factory for creating database connections to all data sources.
|
|
/// </summary>
|
|
public class DbConnectionFactory : IDbConnectionFactory
|
|
{
|
|
private readonly IConfiguration _configuration;
|
|
private readonly ILogger<DbConnectionFactory> _logger;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="DbConnectionFactory"/> class.
|
|
/// </summary>
|
|
/// <param name="configuration">Application configuration.</param>
|
|
/// <param name="logger">Logger instance.</param>
|
|
public DbConnectionFactory(IConfiguration configuration, ILogger<DbConnectionFactory> logger)
|
|
{
|
|
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
|
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public async Task<SqlConnection> CreateLotFinderConnectionAsync(CancellationToken ct = default)
|
|
{
|
|
const string dataSource = "LotFinderDB";
|
|
var connectionString = _configuration.GetConnectionString(dataSource);
|
|
|
|
if (string.IsNullOrEmpty(connectionString))
|
|
{
|
|
throw new ConnectionException(
|
|
$"{dataSource}: Connection string not found in configuration.",
|
|
dataSource);
|
|
}
|
|
|
|
try
|
|
{
|
|
_logger.LogDebug("Creating connection to {DataSource}", dataSource);
|
|
var connection = new SqlConnection(connectionString);
|
|
await connection.OpenAsync(ct).ConfigureAwait(false);
|
|
_logger.LogDebug("Successfully connected to {DataSource}", dataSource);
|
|
return connection;
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
throw;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
using (_logger.BeginScope(new Dictionary<string, object>
|
|
{
|
|
["DataSource"] = dataSource,
|
|
["Operation"] = "CreateConnection"
|
|
}))
|
|
{
|
|
_logger.LogError(ex, "Failed to connect to {DataSource}", dataSource);
|
|
}
|
|
|
|
throw new ConnectionException(
|
|
$"{dataSource}: Failed to open connection to database.",
|
|
dataSource,
|
|
ex);
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public async Task<OracleConnection> CreateJdeConnectionAsync(CancellationToken ct = default)
|
|
{
|
|
return await CreateOracleConnectionAsync("JDE", ct).ConfigureAwait(false);
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public async Task<OracleConnection> CreateJdeStageConnectionAsync(CancellationToken ct = default)
|
|
{
|
|
return await CreateOracleConnectionAsync("JDEStage", ct).ConfigureAwait(false);
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public async Task<OracleConnection> CreateCmsConnectionAsync(CancellationToken ct = default)
|
|
{
|
|
return await CreateOracleConnectionAsync("CMS", ct).ConfigureAwait(false);
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public async Task<OracleConnection> CreateGiwConnectionAsync(CancellationToken ct = default)
|
|
{
|
|
return await CreateOracleConnectionAsync("GIW", ct).ConfigureAwait(false);
|
|
}
|
|
|
|
private async Task<OracleConnection> CreateOracleConnectionAsync(string dataSource, CancellationToken ct)
|
|
{
|
|
var connectionString = _configuration.GetConnectionString(dataSource);
|
|
|
|
if (string.IsNullOrEmpty(connectionString))
|
|
{
|
|
throw new ConnectionException(
|
|
$"{dataSource}: Connection string not found in configuration.",
|
|
dataSource);
|
|
}
|
|
|
|
try
|
|
{
|
|
_logger.LogDebug("Creating connection to {DataSource}", dataSource);
|
|
var connection = new OracleConnection(connectionString);
|
|
await connection.OpenAsync(ct).ConfigureAwait(false);
|
|
_logger.LogDebug("Successfully connected to {DataSource}", dataSource);
|
|
return connection;
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
throw;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
using (_logger.BeginScope(new Dictionary<string, object>
|
|
{
|
|
["DataSource"] = dataSource,
|
|
["Operation"] = "CreateConnection"
|
|
}))
|
|
{
|
|
_logger.LogError(ex, "Failed to connect to {DataSource}", dataSource);
|
|
}
|
|
|
|
throw new ConnectionException(
|
|
$"{dataSource}: Failed to open connection to database.",
|
|
dataSource,
|
|
ex);
|
|
}
|
|
}
|
|
}
|