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, Scope);
}
}
private void SetValue(string Key, T Value)
{
ConfigurationCache.Helpers.SetValue(Database, Scope, Key, Value);
}
private T GetValue(string Key, T Default)
{
return ConfigurationCache.Helpers.GetValue(Database, Scope, Key, Default);
}
protected void Set(T Value, [CallerMemberName] string Key = null)
{
if (Key == null)
throw new ArgumentNullException("Key");
SetValue(Key, Value);
}
protected T Get(T Default, [CallerMemberName] string Key = null)
{
if (Key == null)
throw new ArgumentNullException("Key");
return GetValue(Key, Default);
}
protected void SetObsfucated(string Value, [CallerMemberName] string Key = null)
{
Set(Value.Obsfucate(), Key);
}
protected string GetDeobsfucated(string Default, [CallerMemberName] string Key = null)
{
var obsfucatedValue = Get(null, Key);
if (obsfucatedValue == null)
return Default;
else
return obsfucatedValue.Deobsfucate();
}
protected void RemoveScope()
{
ConfigurationCache.RemoveScope(Database, Scope);
}
protected void RemoveItem(string Key)
{
ConfigurationCache.RemoveScopeKey(Database, Scope, Key);
}
[Obsolete("Types are automatically serialized/deserialized if required, use Set(T Value) instead.")]
protected void SetAsJson(T Value, [CallerMemberName] string Key = null)
{
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 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)
{
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 Get(Default, Key);
}
}
}