diff options
Diffstat (limited to 'src/IOL.VippsEcommerce')
| -rw-r--r-- | src/IOL.VippsEcommerce/Models/VippsConfiguration.cs | 20 | ||||
| -rw-r--r-- | src/IOL.VippsEcommerce/Models/VippsConfigurationMode.cs | 25 |
2 files changed, 40 insertions, 5 deletions
diff --git a/src/IOL.VippsEcommerce/Models/VippsConfiguration.cs b/src/IOL.VippsEcommerce/Models/VippsConfiguration.cs index e1b339e..d8b3a0f 100644 --- a/src/IOL.VippsEcommerce/Models/VippsConfiguration.cs +++ b/src/IOL.VippsEcommerce/Models/VippsConfiguration.cs @@ -101,19 +101,29 @@ namespace IOL.VippsEcommerce.Models /// Use environment variables for configuration. /// <para>If this is true, all requested properties are looked for in the environment.</para> /// </summary> - public bool UseEnvironment { get; set; } + public VippsConfigurationMode ConfigurationMode { get; set; } = VippsConfigurationMode.ONLY_OBJECT; /// <summary> /// Get value from configuration, either from Dependency injection or from the environment. /// </summary> /// <param name="key">Configuration key.</param> - /// <param name="fallback">Fallback value if the key is not found or empty.</param> /// <returns>A string containing the configuration value (or a fallback).</returns> - public string GetValue(string key, string fallback = default) { - if (UseEnvironment) { - return Environment.GetEnvironmentVariable(key) ?? fallback; + public string GetValue(string key) { + switch (ConfigurationMode) { + case VippsConfigurationMode.ONLY_OBJECT: + return GetValueFromObject(key); + case VippsConfigurationMode.ONLY_ENVIRONMENT: + return Environment.GetEnvironmentVariable(key); + case VippsConfigurationMode.OBJECT_THEN_ENVIRONMENT: + return GetValueFromObject(key) ?? Environment.GetEnvironmentVariable(key); + case VippsConfigurationMode.ENVIRONMENT_THEN_OBJECT: + return Environment.GetEnvironmentVariable(key) ?? GetValueFromObject(key); + default: + return default; } + } + private string GetValueFromObject(string key) { foreach (var prop in typeof(VippsConfiguration).GetProperties()) { foreach (var attribute in prop.CustomAttributes) { foreach (var argument in attribute.ConstructorArguments) { diff --git a/src/IOL.VippsEcommerce/Models/VippsConfigurationMode.cs b/src/IOL.VippsEcommerce/Models/VippsConfigurationMode.cs new file mode 100644 index 0000000..edff72f --- /dev/null +++ b/src/IOL.VippsEcommerce/Models/VippsConfigurationMode.cs @@ -0,0 +1,25 @@ +namespace IOL.VippsEcommerce.Models +{ + public enum VippsConfigurationMode + { + /// <summary> + /// Only check for values in the configuration object. + /// </summary> + ONLY_OBJECT = 0, + + /// <summary> + /// Check for values in environment, then in the configuration object. + /// </summary> + ENVIRONMENT_THEN_OBJECT = 1, + + /// <summary> + /// Check for values in the configuration object, then in environment. + /// </summary> + OBJECT_THEN_ENVIRONMENT = 2, + + /// <summary> + /// Only check for values in environment. + /// </summary> + ONLY_ENVIRONMENT = 3, + } +}
\ No newline at end of file |
