Initial commit: JDE Scoping Tool migration project
Set up repository with legacy .NET Framework 4.8 source (OLD/), new .NET 10 Blazor solution (NEW/), OpenSpec specifications, documentation, and project configuration.
This commit is contained in:
@@ -0,0 +1,130 @@
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user