From f3ba315f438df0d3b43fbffc219d19fd34043e2c Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 30 Jan 2026 15:29:13 +0000 Subject: [PATCH] Refactor service lifetimes to Singleton and improve testability - Change `ITranslationService`, `ILyricsService`, `IAudioSynthesisService`, `IDiagnosticService`, and validators to Singleton in `Program.cs`. - Update `TranslationService` to support optional `ChatClient` injection for unit testing using `[ActivatorUtilitiesConstructor]`. - Fix `global.json` to match installed SDK version. Co-authored-by: punkouter26 <121304072+punkouter26@users.noreply.github.com> --- global.json | 2 +- src/PoVicTranslate.Web/Program.cs | 16 ++++++++-------- .../Services/TranslationService.cs | 18 ++++++++++++++++++ 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/global.json b/global.json index 570baa6..cca5e9a 100644 --- a/global.json +++ b/global.json @@ -1,6 +1,6 @@ { "sdk": { - "version": "10.0.102", + "version": "10.0.100", "rollForward": "latestPatch", "allowPrerelease": false } diff --git a/src/PoVicTranslate.Web/Program.cs b/src/PoVicTranslate.Web/Program.cs index 2e2ebcf..5cc9b55 100644 --- a/src/PoVicTranslate.Web/Program.cs +++ b/src/PoVicTranslate.Web/Program.cs @@ -116,20 +116,20 @@ builder.Services.Configure(builder.Configuration.GetSection("ApiSettings")); // Register core services -builder.Services.AddScoped(); -builder.Services.AddScoped(); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); // Register validation services builder.Services.AddSingleton(); -builder.Services.AddScoped(); +builder.Services.AddSingleton(); builder.Services.AddSingleton(); // Register diagnostic validators -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); // Register utility services builder.Services.AddSingleton(); diff --git a/src/PoVicTranslate.Web/Services/TranslationService.cs b/src/PoVicTranslate.Web/Services/TranslationService.cs index 8f7920e..e52a967 100644 --- a/src/PoVicTranslate.Web/Services/TranslationService.cs +++ b/src/PoVicTranslate.Web/Services/TranslationService.cs @@ -2,6 +2,7 @@ using Azure.AI.OpenAI; using Microsoft.ApplicationInsights; using Microsoft.ApplicationInsights.DataContracts; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using OpenAI.Chat; using PoVicTranslate.Web.Configuration; @@ -27,10 +28,20 @@ Maintain the sentiment and intent of the original text. Do not add any explanations, only provide the translated text. """; + [ActivatorUtilitiesConstructor] public TranslationService( IOptions apiSettings, TelemetryClient telemetryClient, ILogger logger) + : this(apiSettings, telemetryClient, logger, null) + { + } + + public TranslationService( + IOptions apiSettings, + TelemetryClient telemetryClient, + ILogger logger, + ChatClient? chatClient) { ArgumentNullException.ThrowIfNull(apiSettings); _telemetryClient = telemetryClient; @@ -39,6 +50,13 @@ public TranslationService( var settings = apiSettings.Value; _deploymentName = settings.AzureOpenAIDeploymentName; + if (chatClient is not null) + { + _chatClient = chatClient; + _logger.LogInformation("TranslationService initialized with injected ChatClient"); + return; + } + if (string.IsNullOrWhiteSpace(settings.AzureOpenAIApiKey) || string.IsNullOrWhiteSpace(settings.AzureOpenAIEndpoint) || string.IsNullOrWhiteSpace(settings.AzureOpenAIDeploymentName))