Example #1
0
        public void Set <T>(IConfigurationSection <T> configuration)
            where T : class, IConfigurationSection <T>
        {
            using var context = new DatabaseContext(Options.Options);
            var entity = context.ConfigurationProfiles
                         .Include(p => p.Values)
                         .SingleOrDefault(p => p.ConfigurationSource == $"plugin:{typeof(T).Name}");

            if (entity == null)
            {
                var defaults = new ConfigurationSection <T>
                                   (new ConfigurationValueCollection(), typeof(T).Name);
                context.ConfigurationProfiles.Add(defaults.AsModel($"plugin:{typeof(T).Name}"));
            }
            else
            {
                foreach (var value in entity.Values)
                {
                    string?newValue = configuration
                                      .Values[value.OptionKey].Value.AsConfigurationStringValue();
                    if (newValue != value.Value)
                    {
                        value.Value = newValue;
                        context.Entry(value).State = EntityState.Modified;
                    }
                }
            }

            context.SaveChanges();
        }
Example #2
0
        public IConfigurationSection <T> Get <T>()
            where T : class, IConfigurationSection <T>
        {
            using var context = new DatabaseContext(Options.Options);
            var entity = context.ConfigurationProfiles
                         .Include(p => p.Values)
                         .SingleOrDefault(p => p.ConfigurationSource == $"plugin:{typeof(T).Name}");

            if (entity != null)
            {
                return(entity.AsConfigurationSection <T>());
            }

            var defaults = new ConfigurationSection <T>
                               (new ConfigurationValueCollection(), typeof(T).Name);

            context.ConfigurationProfiles.Add(defaults.AsModel($"plugin:{typeof(T).Name}"));
            context.SaveChanges();
            return(defaults);
        }