Skip to content

Commit 7e5a9a5

Browse files
committed
refactor GetStorageSettings to AddStorageAccountSettings
Related to: aliencube#319
1 parent 29b9486 commit 7e5a9a5

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

src/AzureOpenAIProxy.ApiApp/Extensions/StorageSettingsExtensions.cs

+10-6
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@ public static class StorageSettingsExtensions
1313
/// </summary>
1414
/// <param name="services"><see cref="IServiceCollection"/> instance.</param>
1515
/// <returns>Returns <see cref="StorageAccountSettings"/> instance.</returns>
16-
public static StorageAccountSettings GetStorageSettings(this IServiceCollection services)
16+
public static IServiceCollection AddStorageAccountSettings(this IServiceCollection services)
1717
{
18-
var configuration = services.BuildServiceProvider().GetService<IConfiguration>()
19-
?? throw new InvalidOperationException($"{nameof(IConfiguration)} service is not registered.");
18+
services.AddSingleton<StorageAccountSettings>(sp => {
19+
var configuration = sp.GetService<IConfiguration>()
20+
?? throw new InvalidOperationException($"{nameof(IConfiguration)} service is not registered.");
2021

21-
var settings = configuration.GetSection(StorageAccountSettings.Name).Get<StorageAccountSettings>()
22-
?? throw new InvalidOperationException($"{nameof(StorageAccountSettings)} could not be retrieved from the configuration.");
22+
var settings = configuration.GetSection(StorageAccountSettings.Name).Get<StorageAccountSettings>()
23+
?? throw new InvalidOperationException($"{nameof(StorageAccountSettings)} could not be retrieved from the configuration.");
2324

24-
return settings;
25+
return settings;
26+
});
27+
28+
return services;
2529
}
2630
}

src/AzureOpenAIProxy.ApiApp/Program.cs

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
// Add OpenAPI service
1717
builder.Services.AddOpenApiService();
1818

19+
// Add Storage Account settings
20+
builder.Services.AddStorageAccountSettings();
21+
1922
// Add TableStorageClient
2023
builder.Services.AddTableStorageService();
2124

src/AzureOpenAIProxy.ApiApp/Repositories/AdminEventRepository.cs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using AzureOpenAIProxy.ApiApp.Configurations;
1+
using Azure.Data.Tables;
2+
3+
using AzureOpenAIProxy.ApiApp.Configurations;
24
using AzureOpenAIProxy.ApiApp.Extensions;
35
using AzureOpenAIProxy.ApiApp.Models;
46

@@ -41,9 +43,10 @@ public interface IAdminEventRepository
4143
/// <summary>
4244
/// This represents the repository entity for the admin event.
4345
/// </summary>
44-
public class AdminEventRepository(IServiceCollection sc) : IAdminEventRepository
46+
public class AdminEventRepository(TableServiceClient tableServiceClient, StorageAccountSettings storageAccountSettings) : IAdminEventRepository
4547
{
46-
private readonly StorageAccountSettings _storageSettings = sc.GetStorageSettings();
48+
private readonly TableServiceClient _tableServiceClient = tableServiceClient;
49+
private readonly StorageAccountSettings _storageAccountSettings = storageAccountSettings;
4750

4851
/// <inheritdoc />
4952
public async Task<AdminEventDetails> CreateEvent(AdminEventDetails eventDetails)
@@ -82,7 +85,7 @@ public static class AdminEventRepositoryExtensions
8285
/// <returns>Returns <see cref="IServiceCollection"/> instance.</returns>
8386
public static IServiceCollection AddAdminEventRepository(this IServiceCollection services)
8487
{
85-
services.AddScoped<IAdminEventRepository>(p => new AdminEventRepository(services));
88+
services.AddScoped<IAdminEventRepository, AdminEventRepository>();
8689

8790
return services;
8891
}

0 commit comments

Comments
 (0)