| | | 1 | | using ArturRios.Extensions; |
| | | 2 | | using Microsoft.Extensions.Configuration; |
| | | 3 | | |
| | | 4 | | namespace ArturRios.Configuration.Providers; |
| | | 5 | | |
| | | 6 | | using IConfigurationProvider = Interfaces.IConfigurationProvider; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Provides configuration values from an <see cref="IConfiguration"/> source (e.g., appsettings.json, environment varia |
| | | 10 | | /// </summary> |
| | | 11 | | /// <remarks> |
| | | 12 | | /// Values are retrieved as strings and parsed using helper extension methods into booleans, integers, or deserialized o |
| | | 13 | | /// </remarks> |
| | | 14 | | /// <param name="configuration">The configuration to read from.</param> |
| | | 15 | | /// <exception cref="ArgumentNullException"><paramref name="configuration"/> is <c>null</c>.</exception> |
| | 9 | 16 | | public class SettingsProvider(IConfiguration configuration) : IConfigurationProvider |
| | | 17 | | { |
| | 10 | 18 | | private readonly IConfiguration _configuration = |
| | 10 | 19 | | configuration ?? throw new ArgumentNullException(nameof(configuration)); |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Gets a boolean value for the given configuration key. |
| | | 23 | | /// </summary> |
| | | 24 | | /// <param name="key">The configuration key.</param> |
| | | 25 | | /// <returns>The parsed boolean value, or <c>null</c> if not found or unparseable.</returns> |
| | | 26 | | public bool? GetBool(string key) |
| | | 27 | | { |
| | 4 | 28 | | var value = _configuration[key]; |
| | | 29 | | |
| | 4 | 30 | | return value.ParseToBoolOrDefault(); |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Gets an integer value for the given configuration key. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="key">The configuration key.</param> |
| | | 37 | | /// <returns>The parsed integer value, or <c>null</c> if not found or unparseable.</returns> |
| | | 38 | | public int? GetInt(string key) |
| | | 39 | | { |
| | 3 | 40 | | var value = _configuration[key]; |
| | | 41 | | |
| | 3 | 42 | | return value.ParseToIntOrDefault(); |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Gets the raw string value for the given configuration key. |
| | | 47 | | /// </summary> |
| | | 48 | | /// <param name="key">The configuration key.</param> |
| | | 49 | | /// <returns>The string value, or <c>null</c> if not found.</returns> |
| | 3 | 50 | | public string? GetString(string key) => _configuration[key]; |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Gets a deserialized object of type <typeparamref name="T"/> for the given configuration key. |
| | | 54 | | /// </summary> |
| | | 55 | | /// <typeparam name="T">The target type to deserialize to.</typeparam> |
| | | 56 | | /// <param name="key">The configuration key.</param> |
| | | 57 | | /// <returns>The deserialized object instance, or <c>null</c> if not found or unparseable.</returns> |
| | | 58 | | /// <remarks> |
| | | 59 | | /// The value stored under <paramref name="key"/> must itself be a JSON string. A section such as |
| | | 60 | | /// <c>"Database": { "Host": "..." }</c> in an appsettings file has no value of its own — its children do — |
| | | 61 | | /// so it reads as <c>null</c>; bind a section with <c>configuration.GetSection(key).Get<T>()</c> from |
| | | 62 | | /// Microsoft.Extensions.Configuration.Binder instead. |
| | | 63 | | /// </remarks> |
| | | 64 | | public T? GetObject<T>(string key) where T : class |
| | | 65 | | { |
| | 4 | 66 | | var value = _configuration[key]; |
| | | 67 | | |
| | 4 | 68 | | return value.ParseToObjectOrDefault<T>(); |
| | | 69 | | } |
| | | 70 | | } |