| | | 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> |
| | 8 | 14 | | public class SettingsProvider(IConfiguration configuration) : IConfigurationProvider |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Gets a boolean value for the given configuration key. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <param name="key">The configuration key.</param> |
| | | 20 | | /// <returns>The parsed boolean value, or <c>null</c> if not found or unparseable.</returns> |
| | | 21 | | public bool? GetBool(string key) |
| | 4 | 22 | | { |
| | 4 | 23 | | var value = configuration[key]; |
| | | 24 | | |
| | 4 | 25 | | return value.ParseToBoolOrDefault(); |
| | 4 | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Gets an integer value for the given configuration key. |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="key">The configuration key.</param> |
| | | 32 | | /// <returns>The parsed integer value, or <c>null</c> if not found or unparseable.</returns> |
| | | 33 | | public int? GetInt(string key) |
| | 3 | 34 | | { |
| | 3 | 35 | | var value = configuration[key]; |
| | | 36 | | |
| | 3 | 37 | | return value.ParseToIntOrDefault(); |
| | 3 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Gets the raw string value for the given configuration key. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <param name="key">The configuration key.</param> |
| | | 44 | | /// <returns>The string value, or <c>null</c> if not found.</returns> |
| | 2 | 45 | | public string? GetString(string key) => configuration[key]; |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Gets a deserialized object of type <typeparamref name="T"/> for the given configuration key. |
| | | 49 | | /// </summary> |
| | | 50 | | /// <typeparam name="T">The target type to deserialize to.</typeparam> |
| | | 51 | | /// <param name="key">The configuration key.</param> |
| | | 52 | | /// <returns>The deserialized object instance, or <c>null</c> if not found or unparseable.</returns> |
| | | 53 | | public T? GetObject<T>(string key) where T : class |
| | 3 | 54 | | { |
| | 3 | 55 | | var value = configuration[key]; |
| | | 56 | | |
| | 3 | 57 | | return value.ParseToObjectOrDefault<T>(); |
| | 3 | 58 | | } |
| | | 59 | | } |