public void GivenADateValueReturnsThatValueAsDate() { _fakeRepository.Get("SomeDate").Returns("12/11/2013"); // The Default DateTimeValueConverter uses the current culture to parse the date // so we supply one with the culture explicity set to AU. // This will be prefered over the default one. var cultureInfo = new CultureInfo("en-au", false); var valueConverters = new ISettingValueConverter[] { new DateTimeValueConverter(cultureInfo) }; var settings = new NinjaSettings <ITestAppSettings>(_fakeRepository, valueConverters).Settings; settings.SomeDate.ShouldBe(new DateTime(2013, 11, 12)); }
private string convertForSerialization(ISetting setting) { ISettingConverter settingConverter = getConverterForSetting(setting); if (settingConverter != null) { return(settingConverter.Serialize(setting)); } ISettingValueConverter valueConverter = getConverterForSettingValue(setting); if (valueConverter != null) { return(valueConverter.Serialize(setting.ObjValue)); } return(setting.ObjValue?.ToString()); }
private void convertForDeserialization(ISetting setting, string serialized) { ISettingConverter settingConverter = getConverterForSetting(setting); if (settingConverter != null) { settingConverter.Deserialize(setting, serialized); return; } ISettingValueConverter valueConverter = getConverterForSettingValue(setting); if (valueConverter != null) { setting.ObjValue = valueConverter.Deserialize(serialized); return; } setting.ObjValue = serialized; }
/// <summary> /// Gets a configuration value from a scope and name. /// </summary> /// <typeparam name="T">Type of the returned value.</typeparam> /// <param name="configuration">Configuration containing a list of key-value pairs.</param> /// <param name="scope">Scope of the configuration.</param> /// <param name="name">Name of the scope.</param> /// <param name="valueConverter">A <see cref="ISettingValueConverter{T}"/> that converts configuration value literals to the type <typeparamref name="T"/>.</param> /// <returns>A configuration value of the type <typeparamref name="T"/></returns> public static T GetScopeValue <T>(this IConfiguration configuration, string scope, string name, ISettingValueConverter <T> valueConverter = null) { string key = ConfigurationUtility.MergeToKey(scope, name); T rtn = GetValue(configuration, key, valueConverter); return(rtn); }
/// <summary> /// Gets a list of value from a <see cref="IConfiguration"/>. Returns <paramref name="defaultValueList"/> if no key was found. /// </summary> /// <typeparam name="T">Type of the returned value.</typeparam> /// <param name="configuration">Configuration containing a list of key-list of value.</param> /// <param name="key">The configuration key.</param> /// <param name="defaultValueList">Default value list to return if no key is found.</param> /// <param name="valueConverter">A <see cref="ISettingValueConverter{T}"/> that converts configuration value literals to the type <typeparamref name="T"/>.</param> /// <returns>A <see cref="IEnumerable{T}"/>.</returns> public static IEnumerable <T> GetValueList <T>(this IConfiguration configuration, string key, IEnumerable <T> defaultValueList, ISettingValueConverter <T> valueConverter = null) => ConfigurationUtility.GetValueList(configuration, key, true, defaultValueList, valueConverter);
/// <summary> /// Gets a value from a <see cref="IConfiguration"/>. Returns <paramref name="defaultValue"/> if no key was found. /// </summary> /// <typeparam name="T">Type of the returned value.</typeparam> /// <param name="configuration">Configuration containing a list of key-value pairs.</param> /// <param name="key">The configuration key.</param> /// <param name="defaultValue">Default value to return if no key is found.</param> /// <param name="valueConverter">A <see cref="ISettingValueConverter{T}"/> that converts configuration value literals to the type <typeparamref name="T"/>.</param> /// <returns>A value of the type <typeparamref name="T"/>.</returns> public static T GetValue <T>(this IConfiguration configuration, string key, T defaultValue, ISettingValueConverter <T> valueConverter = null) => ConfigurationUtility.GetValue(configuration, key, true, defaultValue, valueConverter);
/// <summary> /// Gets a configuration value list from a scope and name. Returns <paramref name="defaultValueList"/> if no key was found. /// </summary> /// <typeparam name="T">Type of the returned value.</typeparam> /// <param name="configuration">Configuration containing a list of key-list of value.</param> /// <param name="scope">Scope of the configuration.</param> /// <param name="name">Name of the scope.</param> /// <param name="defaultValueList">Default value list to return if no key is found.</param> /// <param name="valueConverter">A <see cref="ISettingValueConverter{T}"/> that converts configuration value literals to the type <typeparamref name="T"/>.</param> /// <returns>A <see cref="IEnumerable{T}"/>.</returns> public static IEnumerable <T> GetScopeValueList <T>(this IConfiguration configuration, string scope, string name, IEnumerable <T> defaultValueList, ISettingValueConverter <T> valueConverter = null) { string key = ConfigurationUtility.MergeToKey(scope, name); IEnumerable <T> rtn = GetValueList(configuration, key, defaultValueList, valueConverter); return(rtn); }
/// <summary> /// Gets a configuration value list. /// </summary> /// <typeparam name="T">Type of the returned value.</typeparam> /// <param name="nameValueCollection">Configuration containing a list of key-value pairs or list values.</param> /// <param name="key">The configuration key.</param> /// <param name="valueConverter">A <see cref="ISettingValueConverter{T}"/> that converts configuration value literals to the type <typeparamref name="T"/>.</param> /// <returns>A <see cref="IEnumerable{T}"/>.</returns> public static IEnumerable <T> GetValueList <T>(this NameValueCollection nameValueCollection, string key, ISettingValueConverter <T> valueConverter = null) => ConfigurationUtility.GetValueList(nameValueCollection, key, false, default, valueConverter);
private Type getTypeForValueConverter(ISettingValueConverter converter) => converter.GetType().GetAttribute <SettingValueConverterAttribute>()?.Type;