From 4ea57b2068169dd8e38e4e6a7987388ffc6fc5b4 Mon Sep 17 00:00:00 2001 From: Anatolii Grynchuk Date: Sat, 2 May 2026 00:30:19 +0300 Subject: [PATCH] feat: introduce AppSettings per application - AppSettings class in Api and Worker with SectionName constant - appsettings.json: replaced ConnectionStrings section with App section - Program.cs: bind AppSettings at startup, register as singleton - Connection string now sourced from AppSettings.ConnectionString Ref: IT-628 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- HrynCo.NotificationService.Api/AppSettings.cs | 8 ++++++++ HrynCo.NotificationService.Api/Program.cs | 12 +++++++----- HrynCo.NotificationService.Api/appsettings.json | 4 ++-- HrynCo.NotificationService.Worker/AppSettings.cs | 8 ++++++++ HrynCo.NotificationService.Worker/Program.cs | 8 +++++--- HrynCo.NotificationService.Worker/appsettings.json | 4 ++-- 6 files changed, 32 insertions(+), 12 deletions(-) create mode 100644 HrynCo.NotificationService.Api/AppSettings.cs create mode 100644 HrynCo.NotificationService.Worker/AppSettings.cs diff --git a/HrynCo.NotificationService.Api/AppSettings.cs b/HrynCo.NotificationService.Api/AppSettings.cs new file mode 100644 index 0000000..a6d15cd --- /dev/null +++ b/HrynCo.NotificationService.Api/AppSettings.cs @@ -0,0 +1,8 @@ +namespace HrynCo.NotificationService.Api; + +public sealed class AppSettings +{ + public const string SectionName = "App"; + + public string ConnectionString { get; init; } = string.Empty; +} \ No newline at end of file diff --git a/HrynCo.NotificationService.Api/Program.cs b/HrynCo.NotificationService.Api/Program.cs index 2798628..95b1753 100644 --- a/HrynCo.NotificationService.Api/Program.cs +++ b/HrynCo.NotificationService.Api/Program.cs @@ -1,3 +1,4 @@ +using HrynCo.NotificationService.Api; using HrynCo.NotificationService.DAL.EF; using HrynCo.NotificationService.Services; using Serilog; @@ -7,13 +8,14 @@ var builder = WebApplication.CreateBuilder(args); builder.Host.UseSerilog((context, lc) => lc.ReadFrom.Configuration(context.Configuration)); +var appSettings = builder.Configuration + .GetSection(AppSettings.SectionName) + .Get() ?? throw new InvalidOperationException("App settings are not configured."); + +builder.Services.AddSingleton(appSettings); builder.Services.AddOpenApi(); builder.Services.AddControllers(); - -string connectionString = builder.Configuration.GetConnectionString("Default") - ?? throw new InvalidOperationException("Connection string 'Default' is not configured."); - -builder.Services.AddNotificationDataAccess(connectionString); +builder.Services.AddNotificationDataAccess(appSettings.ConnectionString); builder.Services.AddNotificationServices(); var app = builder.Build(); diff --git a/HrynCo.NotificationService.Api/appsettings.json b/HrynCo.NotificationService.Api/appsettings.json index 253cb2d..c78a6b6 100644 --- a/HrynCo.NotificationService.Api/appsettings.json +++ b/HrynCo.NotificationService.Api/appsettings.json @@ -1,6 +1,6 @@ { - "ConnectionStrings": { - "Default": "Host=localhost;Port=5432;Database=notification_service;Username=postgres;Password=postgres" + "App": { + "ConnectionString": "Host=localhost;Port=5432;Database=notification_service;Username=postgres;Password=postgres" }, "Serilog": { "MinimumLevel": { diff --git a/HrynCo.NotificationService.Worker/AppSettings.cs b/HrynCo.NotificationService.Worker/AppSettings.cs new file mode 100644 index 0000000..c8cd459 --- /dev/null +++ b/HrynCo.NotificationService.Worker/AppSettings.cs @@ -0,0 +1,8 @@ +namespace HrynCo.NotificationService.Worker; + +public sealed class AppSettings +{ + public const string SectionName = "App"; + + public string ConnectionString { get; init; } = string.Empty; +} \ No newline at end of file diff --git a/HrynCo.NotificationService.Worker/Program.cs b/HrynCo.NotificationService.Worker/Program.cs index bb7026a..bebce7a 100644 --- a/HrynCo.NotificationService.Worker/Program.cs +++ b/HrynCo.NotificationService.Worker/Program.cs @@ -8,10 +8,12 @@ var builder = Host.CreateApplicationBuilder(args); builder.Services.AddSerilog(lc => lc.ReadFrom.Configuration(builder.Configuration)); -string connectionString = builder.Configuration.GetConnectionString("Default") - ?? throw new InvalidOperationException("Connection string 'Default' is not configured."); +var appSettings = builder.Configuration + .GetSection(AppSettings.SectionName) + .Get() ?? throw new InvalidOperationException("App settings are not configured."); -builder.Services.AddNotificationDataAccess(connectionString); +builder.Services.AddSingleton(appSettings); +builder.Services.AddNotificationDataAccess(appSettings.ConnectionString); builder.Services.AddNotificationServices(); builder.Services.AddHostedService(); diff --git a/HrynCo.NotificationService.Worker/appsettings.json b/HrynCo.NotificationService.Worker/appsettings.json index 117dbc3..67e3bf3 100644 --- a/HrynCo.NotificationService.Worker/appsettings.json +++ b/HrynCo.NotificationService.Worker/appsettings.json @@ -1,6 +1,6 @@ { - "ConnectionStrings": { - "Default": "Host=localhost;Port=5432;Database=notification_service;Username=postgres;Password=postgres" + "App": { + "ConnectionString": "Host=localhost;Port=5432;Database=notification_service;Username=postgres;Password=postgres" }, "Serilog": { "MinimumLevel": {