public static ISettingsBuilder AddMemoryAdapter(
     this ISettingsBuilder builder,
     IDictionary <string, object> data = null,
     ISettingConverter converter       = null)
 {
     return(builder.Add(new MemoryAdapter(data), converter));
 }
Example #2
0
 public static ISettingsBuilder LoadFromDictionary(
     this ISettingsBuilder builder,
     IDictionary <string, object> dict,
     ISettingConverter converter = null)
 {
     return(builder.Load(new DictionaryLoader(dict), converter));
 }
Example #3
0
 public static ISettingsBuilder AddEnvironmentVariableAdapter(
     this ISettingsBuilder builder,
     string prefix = null,
     ISettingConverter converter = null)
 {
     return(builder.Add(new EnvironmentVariableAdapter(prefix), converter));
 }
Example #4
0
        /// <inheritdoc />
        public Dictionary <string, List <object> > GetValues(Predicate <string> predicate, Type type)
        {
            if (predicate == null)
            {
                throw new ArgumentNullException(nameof(predicate));
            }

            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            ISettingConverter converter = this.GetConverterForType(type);

            if (converter == null)
            {
                throw new InvalidOperationException($"No converter found for type {type.Name}");
            }

            Dictionary <string, List <string> > stringValues = this.GetRawValues(predicate);

            Dictionary <string, List <object> > finalValues =
                new Dictionary <string, List <object> >(SettingService.NameComparer);

            foreach (KeyValuePair <string, List <string> > stringValue in stringValues)
            {
                finalValues.Add(stringValue.Key,
                                new List <object>(stringValue.Value.Select(x => this.ConvertTo(converter, type, x))));
            }

            return(finalValues);
        }
Example #5
0
        public SqlServer(string nameOrConnectionString, ISettingConverter converter) : base(new SettingNameFactory(), converter)
        {
            ConnectionString = ConnectionStringRepository.Default.GetConnectionString(nameOrConnectionString);

            SettingTableName = (DefaultSchema, DefaultTable);
            ColumnMapping    = new SqlServerColumnMapping();
        }
Example #6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SettingsServiceBuilder"/> class.
 /// </summary>
 public SettingsServiceBuilder()
 {
     this.appKeyPrefix    = string.Empty;
     this.settingsPostfix = "Settings";
     this.converter       = new DefaultSettingConverter();
     this.handler         = new ThrowOnInvalidSettingHandler();
 }
Example #7
0
        /// <inheritdoc />
        public List <object> GetValues(string name, Type type)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException("The string is empty.", nameof(name));
            }

            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            ISettingConverter converter = this.GetConverterForType(type);

            if (converter == null)
            {
                throw new InvalidOperationException($"No converter found for type {type.Name}");
            }

            List <string> stringValues = this.GetRawValues(name);

            List <object> finalValues = stringValues.Select(x => this.ConvertTo(converter, type, x))
                                        .ToList();

            return(finalValues);
        }
Example #8
0
 public static ISettingsBuilder AddReadOnlyRegistryAdapter(
     this ISettingsBuilder builder,
     string root,
     ISettingConverter converter = null)
 {
     return(builder.AddReadOnly(new RegistryAdapter(root), converter));
 }
 public static ISettingsBuilder LoadFromObject <T>(
     this ISettingsBuilder builder,
     T data,
     ISettingConverter converter = null)
 {
     return(builder.Load(new ObjectLoader <T>(data), converter));
 }
Example #10
0
        public ISettingsBuilder Add(ISettingsAdapter adapter, ISettingConverter converter)
        {
            converter = converter ?? new DefaultSettingConverter();
            var interceptor = new SettingsInterceptor(adapter, converter);

            _interceptors.Add(interceptor);
            return(this);
        }
Example #11
0
 public static ISettingsBuilder LoadFromCommandLine(
     this ISettingsBuilder builder,
     IEnumerable <string> args,
     IDictionary <string, string> switchMappings = null,
     ISettingConverter converter = null)
 {
     return(builder.Load(new CommandLineLoader(args, switchMappings), converter));
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="AspNetCoreSettingsService"/> class.
 /// </summary>
 /// <param name="configuration">The configuration.</param>
 /// <param name="appKeyPrefix">The application key prefix.</param>
 /// <param name="settingsPostfix">The settings postfix.</param>
 /// <param name="converter">The converter.</param>
 /// <param name="handler">The handler.</param>
 public AspNetCoreSettingsService(
     IConfiguration configuration,
     string appKeyPrefix            = "",
     string settingsPostfix         = "Settings",
     ISettingConverter converter    = null,
     IInvalidSettingHandler handler = null)
     : base(new AspNetCoreSettingsSource(configuration), appKeyPrefix, settingsPostfix, converter, handler)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="AppConfigSettingsService"/> class.
 /// </summary>
 /// <param name="settings">The settings.</param>
 /// <param name="appKeyPrefix">The application key prefix.</param>
 /// <param name="settingsPostfix">The settings postfix.</param>
 /// <param name="converter">The converter.</param>
 /// <param name="handler">The handler.</param>
 public AppConfigSettingsService(
     NameValueCollection settings,
     string appKeyPrefix            = "",
     string settingsPostfix         = "Settings",
     ISettingConverter converter    = null,
     IInvalidSettingHandler handler = null)
     : base(new AppConfigSettingsSource(settings), appKeyPrefix, settingsPostfix, converter, handler)
 {
 }
Example #14
0
        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());
        }
Example #15
0
        private string ConvertFrom(ISettingConverter converter, Type type, object value)
        {
            Type usedType = this.GetConverterType(type);
            bool nullable = this.IsNullable(type);

            if (nullable && (value == null))
            {
                return(string.Empty);
            }

            if (value == null)
            {
                return(null);
            }

            return(converter.ConvertFrom(usedType, value));
        }
Example #16
0
        private object ConvertTo(ISettingConverter converter, Type type, string value)
        {
            Type usedType = this.GetConverterType(type);
            bool nullable = this.IsNullable(type);

            if (value == null)
            {
                return(null);
            }

            if (nullable && string.IsNullOrEmpty(value))
            {
                return(null);
            }

            return(converter.ConvertTo(usedType, value));
        }
Example #17
0
        /// <inheritdoc />
        public void RemoveConverter(ISettingConverter settingConverter)
        {
            if (settingConverter == null)
            {
                throw new ArgumentNullException(nameof(settingConverter));
            }

            if (!this.Converters.Contains(settingConverter))
            {
                return;
            }

            Trace.TraceInformation($"Removing setting converter: {settingConverter.GetType().Name}");

            this.Cache.Clear();

            this.Converters.Remove(settingConverter);
        }
Example #18
0
        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>
        /// Initializes a new instance of the <see cref="DefaultSettingsService"/> class.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="appKeyPrefix">The application key prefix.</param>
        /// <param name="settingsPostfix">The settings postfix.</param>
        /// <param name="converter">The converter.</param>
        /// <param name="handler">The handler.</param>
        public DefaultSettingsService(
            ISettingsSource source,
            string appKeyPrefix            = "",
            string settingsPostfix         = "Settings",
            ISettingConverter converter    = null,
            IInvalidSettingHandler handler = null)
        {
            if (string.IsNullOrWhiteSpace(settingsPostfix))
            {
                throw new ArgumentNullException(nameof(settingsPostfix));
            }

            this.appKeyPrefix    = appKeyPrefix;
            this.settingsPostfix = settingsPostfix;
            this.source          = source ?? throw new ArgumentNullException(nameof(source));
            this.converter       = converter ?? new DefaultSettingConverter();
            this.handler         = handler ?? new ThrowOnInvalidSettingHandler();
        }
Example #20
0
        /// <inheritdoc />
        public void SetValues(string name, IEnumerable values, Type type)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException("The string is empty.", nameof(name));
            }

            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            ISettingConverter converter = this.GetConverterForType(type);

            if (converter == null)
            {
                throw new InvalidOperationException($"No converter found for type {type.Name}");
            }

            if (values == null)
            {
                this.DeleteValues(name);
                return;
            }

            List <string> finalValues = values.Cast <object>()
                                        .Select(x => this.ConvertFrom(converter, type, x))
                                        .ToList();

            if (finalValues.Count == 0)
            {
                this.DeleteValues(name);
                return;
            }

            this.SetRawValues(name, finalValues);
        }
Example #21
0
 protected SettingProvider([NotNull] ISettingNameFactory settingNameFactory, [NotNull] ISettingConverter converter)
     : this()
 {
     _converter          = converter ?? throw new ArgumentNullException(nameof(converter));
     _settingNameFactory = settingNameFactory ?? throw new ArgumentNullException(nameof(settingNameFactory));
 }
Example #22
0
        public ISettingsBuilder Load(ISettingsLoader loader, ISettingConverter converter)
        {
            var data = loader.Load();

            return(AddReadOnly(new MemoryAdapter(data), converter));
        }
Example #23
0
        public ISettingsBuilder AddReadOnly(ISettingsAdapter adapter, ISettingConverter converter)
        {
            var readOnlyAdapter = new ReadOnlyAdapter(adapter);

            return(Add(readOnlyAdapter, converter));
        }
 public static ExpressionLoaderSettingsBuilder <TMapper> StartMapping <TMapper>(
     this ISettingsBuilder builder,
     ISettingConverter converter = null)
 {
     return(new ExpressionLoaderSettingsBuilder <TMapper>(builder, converter));
 }
Example #25
0
 public AppSettings(ISettingConverter converter) : base(new SettingNameFactory(), converter)
 {
 }
Example #26
0
 public ISettingsBuilder AddReadOnly(ISettingsAdapter adapter, ISettingConverter converter)
 {
     _builder.Load(_expressionLoader, _converter);
     return(_builder.AddReadOnly(adapter, converter));
 }
Example #27
0
 public ISettingsBuilder Load(ISettingsLoader loader, ISettingConverter converter)
 {
     _builder.Load(_expressionLoader, _converter);
     return(_builder.Load(loader, converter));
 }
Example #28
0
 private Type getTypeForSettingConverter(ISettingConverter converter)
 => converter.GetType().GetAttribute <SettingConverterAttribute>()?.Type;
Example #29
0
 public ExpressionLoaderSettingsBuilder(ISettingsBuilder builder, ISettingConverter converter)
 {
     _builder          = builder;
     _expressionLoader = new ExpressionLoader <TMapper>();
     _converter        = converter;
 }
Example #30
0
 public SettingsInterceptor(ISettingsAdapter adapter, ISettingConverter converter)
 {
     _adapter   = adapter;
     _converter = converter;
 }