From def73c3fa89d656974e64fe8d4f81e3448933fd4 Mon Sep 17 00:00:00 2001 From: Hermes Agent Date: Sun, 16 Aug 2026 10:33:12 +0200 Subject: [PATCH] Added instrastructure extensions Implemented database service registration --- .../Database/ApplicationDbContextFactory.cs | 6 ++++-- .../Extensions/Constants.cs | 6 ++++++ .../Extensions/Postgres.cs | 17 +++++++++++++++++ .../PostFundManagement.Infrastructure.csproj | 3 ++- 4 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 PostFundManagement.Infrastructure/Extensions/Constants.cs create mode 100644 PostFundManagement.Infrastructure/Extensions/Postgres.cs diff --git a/PostFundManagement.Infrastructure/Database/ApplicationDbContextFactory.cs b/PostFundManagement.Infrastructure/Database/ApplicationDbContextFactory.cs index f3f27d7..a11f435 100644 --- a/PostFundManagement.Infrastructure/Database/ApplicationDbContextFactory.cs +++ b/PostFundManagement.Infrastructure/Database/ApplicationDbContextFactory.cs @@ -1,3 +1,5 @@ +using static PostFundManagement.Infrastructure.Extensions.Constants; + namespace PostFundManagement.Infrastructure.Database; public sealed class ApplicationDbContextFactory : IDesignTimeDbContextFactory @@ -12,8 +14,8 @@ public sealed class ApplicationDbContextFactory : IDesignTimeDbContextFactory(); - var connectionString = configuration.GetConnectionString("PfmDatabase") - ?? throw new InvalidOperationException("Connection string 'PfmDatabase' was not found in configuration."); + var connectionString = configuration.GetConnectionString(DatabaseConfigName) + ?? throw new InvalidOperationException($"Connection string '{DatabaseConfigName}' was not found in configuration."); optionsBuilder.UseNpgsql(connectionString, npgsqlOptions => npgsqlOptions.MigrationsAssembly(typeof(ApplicationDbContext).Assembly.FullName)); diff --git a/PostFundManagement.Infrastructure/Extensions/Constants.cs b/PostFundManagement.Infrastructure/Extensions/Constants.cs new file mode 100644 index 0000000..66ac514 --- /dev/null +++ b/PostFundManagement.Infrastructure/Extensions/Constants.cs @@ -0,0 +1,6 @@ +namespace PostFundManagement.Infrastructure.Extensions; + +public static class Constants +{ + public const string DatabaseConfigName = "PfmDatabase"; +} \ No newline at end of file diff --git a/PostFundManagement.Infrastructure/Extensions/Postgres.cs b/PostFundManagement.Infrastructure/Extensions/Postgres.cs new file mode 100644 index 0000000..2e943c1 --- /dev/null +++ b/PostFundManagement.Infrastructure/Extensions/Postgres.cs @@ -0,0 +1,17 @@ +using PostFundManagement.Infrastructure.Database; +using static PostFundManagement.Infrastructure.Extensions.Constants; + +namespace PostFundManagement.Infrastructure.Extensions; + +public static class Postgres +{ + public static IServiceCollection AddApplicationDbContext(this IServiceCollection services, IConfiguration configuration) + { + var connectionString = configuration.GetConnectionString(DatabaseConfigName) + ?? throw new InvalidOperationException($"Connection string '{DatabaseConfigName}' was not found in configuration."); + + services.AddPooledDbContextFactory(builder => builder.UseNpgsql(connectionString)); + + return services; + } +} \ No newline at end of file diff --git a/PostFundManagement.Infrastructure/PostFundManagement.Infrastructure.csproj b/PostFundManagement.Infrastructure/PostFundManagement.Infrastructure.csproj index b0d01a3..7a4e7e4 100644 --- a/PostFundManagement.Infrastructure/PostFundManagement.Infrastructure.csproj +++ b/PostFundManagement.Infrastructure/PostFundManagement.Infrastructure.csproj @@ -27,13 +27,14 @@ - + +