| | | 1 | | |
| | | 2 | | using Microsoft.Extensions.Configuration; |
| | | 3 | | |
| | | 4 | | namespace Snacks.DAL.Common; |
| | | 5 | | public static class ConfigurationUtil |
| | | 6 | | { |
| | | 7 | | private static IConfiguration? configuration = null; |
| | | 8 | | |
| | | 9 | | public static IConfiguration GetConfiguration() => |
| | 204 | 10 | | configuration ??= new ConfigurationBuilder() |
| | 204 | 11 | | .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) |
| | 204 | 12 | | .Build(); |
| | | 13 | | |
| | | 14 | | |
| | | 15 | | public static (string ConnectionString, string ProviderName) GetConnectionParameters(string configName) |
| | 2 | 16 | | { |
| | 2 | 17 | | return GetConnectionParameters(GetConfiguration(), configName); |
| | 2 | 18 | | } |
| | | 19 | | |
| | | 20 | | public static (string ConnectionString, string ProviderName) GetConnectionParameters(IConfiguration configuration, s |
| | 212 | 21 | | { |
| | 212 | 22 | | var connectionConfig = configuration.GetSection("ConnectionStrings").GetSection(configName); |
| | 212 | 23 | | if (!connectionConfig.Exists()) |
| | 4 | 24 | | { |
| | | 25 | | |
| | 4 | 26 | | throw new ArgumentException($"Connection configuration '{configName}' does not exist"); |
| | | 27 | | } |
| | | 28 | | |
| | 208 | 29 | | var connectionString = connectionConfig["ConnectionString"]; |
| | 208 | 30 | | if (connectionString is null) |
| | 2 | 31 | | { |
| | 2 | 32 | | throw new ArgumentException($"Property ConnectionString not defined in configuration '{configName}'."); |
| | | 33 | | } |
| | | 34 | | |
| | 206 | 35 | | var providerName = connectionConfig["ProviderName"]; |
| | 206 | 36 | | if (providerName is null) |
| | 2 | 37 | | { |
| | 2 | 38 | | throw new ArgumentException($"Property ProviderName not defined in configuration '{configName}'."); |
| | | 39 | | } |
| | | 40 | | |
| | 204 | 41 | | return (connectionString, providerName); |
| | 204 | 42 | | } |
| | | 43 | | } |