Files
parr_api/PARR.DAL/Configurations/DbSettings/DBConfigurationProvider.cs
2023-09-19 14:34:55 +10:00

43 lines
1.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using PARR.DAL.Context;
using PARR.DAL.Contracts;
namespace PARR.DAL.Configurations.DbSettings
{
internal class DBConfigurationProvider : ConfigurationProvider
{
private readonly IServiceCollection services;
public DBConfigurationProvider(IServiceCollection services)
{
this.services = services;
}
public override void Load()
{
using (var dataContext = services.BuildServiceProvider().GetService<DataContext>())
{
//если миграциями БД еще не создана, то SettingsInDbs будет недоступна
try
{
var settings = dataContext?.Setting.ToList();
if (settings == null)
return;
if (settings != null)
{
//формируем список с корневым SettingsFromDb, и в него вложены дочерние настройки из БД, чтобы потом забиндить в класс
Data = settings.ToDictionary(settings => $"{nameof(SettingsFromDb)}:{settings.Name}", settings => (string?)settings.Value);
}
}
catch (Exception ex)
{
//TODO: логи, я не тестил, возможно надо через BuildServiceProvider прокинуть ILogger
}
}
}
}
}