using Microsoft.Extensions.Configuration;
namespace DigitalData.Core.Abstractions
{
///
/// Extension methods for the interface, providing
/// additional functionality for retrieving configuration values with default behavior.
///
public static class ConfigurationExtension
{
///
/// Retrieves a configuration value for the specified key, or returns a default value
/// of type if the configuration is not found or the key is null.
///
/// The type of the object to retrieve from the configuration.
/// The instance.
/// The optional key to look for in the configuration. If null, the method
/// retrieves the root configuration.
///
/// An instance of populated from the configuration values, or
/// a new instance of if no matching configuration is found.
///
public static T GetOrDefault(this IConfiguration configuration, string? key = null)
where T : new()
=> (key is null
? configuration.Get()
: configuration.GetSection(key).Get())
?? new T();
}
}