public override object Read(IConfiguration configuration, Type configType, IKeyName keyName)
        {
            var valueType   = configType.GetGenericTypeOfConfigurationSetting();
            var elementType = valueType.GetElementType();
            var key         = keyName.QualifiedKeyName;

            // Making sure the section exists
            if (!configuration.GetSection(key).Exists())
            {
                // Return null if no section exists
                StaticLoggingHelper.Warning($"No configuration for '{key}' was found.");

                return(null);
            }

            // Handle Complex Arrays
            if (valueType.IsArray && elementType != null && !elementType.IsValueType)
            {
                return(configuration.GetSection(key).GetChildren()
                       .Select(config => config.Get(elementType))
                       .ToArray());
            }

            return(configuration.GetSection(key).Get(valueType));
        }
Beispiel #2
0
        /// <summary>
        /// This method gets called when setting the value, use this to customize the logic
        /// </summary>
        /// <param name="value">The value read from the configuration</param>
        public virtual void SetValue(TValue value)
        {
            if (value == null || value.Equals(default(TValue)))
            {
                StaticLoggingHelper.Info($"{GetType().Name} value has been resolved as null/default");
                Value = default(TValue);
            }

            Value = value;
        }
        public override object Read(IConfiguration configuration, Type complexType, IKeyName keyName)
        {
            var key = keyName.QualifiedKeyName;

            // return null if no section exists
            if (!ValueExists(configuration, complexType, keyName))
            {
                StaticLoggingHelper.Warning($"No configuration for '{key}' was found.");
                return(null);
            }

            return(configuration.GetSection(key).Get(complexType));
        }
        public override object Read(IConfiguration configuration, Type configType, IKeyName keyName)
        {
            var key       = keyName.QualifiedKeyName;
            var valueType = configType.GetGenericTypeOfConfigurationSetting();

            // Making sure the section exists
            if (!ValueExists(configuration, configType, keyName))
            {
                // Return default value if no section exists
                StaticLoggingHelper.Warning($"No configuration for '{key}' was found.");
                return(GetDefault(valueType));
            }

            return(configuration.GetSection(key).Get(valueType));
        }
        public dynamic CreateConfigurationSetting(Type configType)
        {
            try
            {
                StaticLoggingHelper.Debug($"{configType} - Trying to resolve value.");

                // Resolve the key and prefix names
                var keyName = _keyNameResolver.Resolve(configType, _configurationOptions);

                // Pick configuration value reader
                var reader = _configurationReaderResolver.Resolve(configType, _configurationOptions);

                // Pick parser
                var parser = _parserResolver.Resolve(configType, _configurationOptions);

                // Pick constructor
                var valueConstructor = _valueConstructorResolver.Resolve(configType, _configurationOptions);

                // Ensure all required instances are present
                keyName.EnsureNotNull(nameof(IKeyName));
                reader.EnsureNotNull(nameof(IConfigurationReader));
                parser.EnsureNotNull(nameof(IParser));
                valueConstructor.EnsureNotNull(nameof(IValueConstructor));

                // Throw exception if missing configuration is required
                if (_configurationOptions.MissingConfigurationStratergy == MissingConfigurationStratergy.ThrowException &&
                    !reader.ValueExists(_configuration, configType, keyName))
                {
                    throw new ConfigurationSettingMissingException(configType, keyName);
                }

                // Read and parse the value
                var rawValue = reader.Read(_configuration, configType, keyName);
                var value    = parser.Parse(configType, rawValue);

                // Set the value
                var configSetting = valueConstructor?.ConstructValue(configType, value);

                StaticLoggingHelper.Debug($"{configType} - Value resolved successfully.");

                return(configSetting);
            }
            catch (Exception e)
            {
                StaticLoggingHelper.Error($"An error occured while attempting to resolve value for {configType}", e);
                throw;
            }
        }