using Disco.Data.Repository; using System; using System.Collections.Generic; using System.Runtime.CompilerServices; namespace Disco.Data.Configuration { public abstract class ConfigurationBase { private DiscoDataContext Database; public abstract string Scope { get; } /// /// Creates a read-write configuration instance /// public ConfigurationBase(DiscoDataContext Database) { this.Database = Database; } /// /// Creates a read-only configuration instance /// public ConfigurationBase() : this(null) { } protected IEnumerable ItemKeys { get { return ConfigurationCache.GetScopeKeys(Database, this.Scope); } } private void SetValue(string Key, T Value) { ConfigurationCache.Helpers.SetValue(Database, this.Scope, Key, Value); } private T GetValue(string Key, T Default) { return ConfigurationCache.Helpers.GetValue(Database, this.Scope, Key, Default); } protected void Set(T Value, [CallerMemberName] string Key = null) { if (Key == null) throw new ArgumentNullException("Key"); this.SetValue(Key, Value); } protected T Get(T Default, [CallerMemberName] string Key = null) { if (Key == null) throw new ArgumentNullException("Key"); return this.GetValue(Key, Default); } protected void SetObsfucated(string Value, [CallerMemberName] string Key = null) { this.Set(Value.Obsfucate(), Key); } protected string GetDeobsfucated(string Default, [CallerMemberName] string Key = null) { var obsfucatedValue = this.Get(null, Key); if (obsfucatedValue == null) return Default; else return obsfucatedValue.Deobsfucate(); } protected void RemoveScope() { ConfigurationCache.RemoveScope(Database, this.Scope); } protected void RemoveItem(string Key) { ConfigurationCache.RemoveScopeKey(Database, this.Scope, Key); } [Obsolete("Types are automatically serialized/deserialized if required, use Set(T Value) instead.")] protected void SetAsJson(T Value, [CallerMemberName] string Key = null) { this.Set(Value, Key); } [Obsolete("Types are automatically serialized/deserialized if required, use Get(T Default) instead.")] protected T GetFromJson(T Default, [CallerMemberName] string Key = null) { return this.Get(Default, Key); } [Obsolete("Types are automatically serialized/deserialized if required, use Set(T Value) instead.")] protected void SetAsEnum(T Value, [CallerMemberName] string Key = null) { this.Set(Value, Key); } [Obsolete("Types are automatically serialized/deserialized if required, use Set(T Value) instead.")] protected T GetFromEnum(T Default, [CallerMemberName] string Key = null) { return this.Get(Default, Key); } } }