mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-07-16 17:47:22 +00:00
code style cleanup
This commit is contained in:
@@ -5,8 +5,8 @@ using Microsoft.Extensions.Configuration;
|
||||
namespace Infrastructure.Sql;
|
||||
|
||||
/// <summary>
|
||||
/// Default <see cref="ISqlConnectionFactory"/> implementation that creates SQL Server connections,
|
||||
/// resolving the connection string from environment variables or application configuration.
|
||||
/// Default <see cref="ISqlConnectionFactory" /> implementation that creates SQL Server connections,
|
||||
/// resolving the connection string from environment variables or application configuration.
|
||||
/// </summary>
|
||||
/// <param name="configuration">The application configuration, used as a fallback source for the connection string.</param>
|
||||
public class DefaultSqlConnectionFactory(IConfiguration configuration)
|
||||
@@ -17,26 +17,32 @@ public class DefaultSqlConnectionFactory(IConfiguration configuration)
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Resolves the SQL Server connection string, preferring (in order): the <c>DB_CONNECTION_STRING</c>
|
||||
/// environment variable, a connection string built from individual <c>DB_*</c> environment variables
|
||||
/// via <see cref="SqlConnectionStringHelper.BuildConnectionString"/>, and finally the <c>"Default"</c>
|
||||
/// connection string from <paramref name="configuration"/>.
|
||||
/// Creates a new, unopened <see cref="SqlConnection" /> using the resolved connection string.
|
||||
/// </summary>
|
||||
/// <returns>A new <see cref="SqlConnection" /> instance.</returns>
|
||||
public DbConnection CreateConnection()
|
||||
{
|
||||
return new SqlConnection(_connectionString);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resolves the SQL Server connection string, preferring (in order): the <c>DB_CONNECTION_STRING</c>
|
||||
/// environment variable, a connection string built from individual <c>DB_*</c> environment variables
|
||||
/// via <see cref="SqlConnectionStringHelper.BuildConnectionString" />, and finally the <c>"Default"</c>
|
||||
/// connection string from <paramref name="configuration" />.
|
||||
/// </summary>
|
||||
/// <param name="configuration">The application configuration to fall back to.</param>
|
||||
/// <returns>The resolved SQL Server connection string.</returns>
|
||||
/// <exception cref="InvalidOperationException">
|
||||
/// Thrown when no connection string can be resolved from any of the supported sources.
|
||||
/// Thrown when no connection string can be resolved from any of the supported sources.
|
||||
/// </exception>
|
||||
private static string GetConnectionString(IConfiguration configuration)
|
||||
{
|
||||
// Check for full connection string first
|
||||
var fullConnectionString = Environment.GetEnvironmentVariable(
|
||||
string? fullConnectionString = Environment.GetEnvironmentVariable(
|
||||
"DB_CONNECTION_STRING"
|
||||
);
|
||||
if (!string.IsNullOrEmpty(fullConnectionString))
|
||||
{
|
||||
return fullConnectionString;
|
||||
}
|
||||
if (!string.IsNullOrEmpty(fullConnectionString)) return fullConnectionString;
|
||||
|
||||
// Try to build from individual environment variables (preferred method for Docker)
|
||||
try
|
||||
@@ -46,24 +52,12 @@ public class DefaultSqlConnectionFactory(IConfiguration configuration)
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
// Fall back to configuration-based connection string if env vars are not set
|
||||
var connString = configuration.GetConnectionString("Default");
|
||||
if (!string.IsNullOrEmpty(connString))
|
||||
{
|
||||
return connString;
|
||||
}
|
||||
string? connString = configuration.GetConnectionString("Default");
|
||||
if (!string.IsNullOrEmpty(connString)) return connString;
|
||||
|
||||
throw new InvalidOperationException(
|
||||
"Database connection string not configured. Set DB_CONNECTION_STRING or DB_SERVER, DB_NAME, DB_USER, DB_PASSWORD env vars or ConnectionStrings:Default."
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new, unopened <see cref="SqlConnection"/> using the resolved connection string.
|
||||
/// </summary>
|
||||
/// <returns>A new <see cref="SqlConnection"/> instance.</returns>
|
||||
public DbConnection CreateConnection()
|
||||
{
|
||||
return new SqlConnection(_connectionString);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user