Update: Configuration Framework +CallerMemberName

Simplified creation of configuration modules; Scope is obtained from
abstract, Property names become keys (via CallerMemberName); Simple
generic Get/Set methods are used; Helpers for Obsfucation and Json are
available.
This commit is contained in:
Gary Sharp
2013-05-03 20:48:15 +10:00
parent d19ad7d0dc
commit 8533ec2fe0
23 changed files with 545 additions and 684 deletions
+65 -11
View File
@@ -1,35 +1,89 @@
using System;
using Disco.Data.Repository;
using Disco.Models.Repository;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
namespace Disco.Data.Configuration
{
public abstract class ConfigurationBase
{
private ConfigurationContext _context;
private DiscoDataContext dbContext;
public ConfigurationContext Context
public abstract string Scope { get; }
public ConfigurationBase(DiscoDataContext dbContext)
{
this.dbContext = dbContext;
}
protected List<ConfigurationItem> Items
{
get
{
return _context;
return ConfigurationCache.GetConfigurationItems(dbContext, this.Scope);
}
}
public abstract string Scope { get; }
public ConfigurationBase(ConfigurationContext Context)
private void SetValue<ValueType>(string Key, ValueType Value)
{
this._context = Context;
ConfigurationCache.SetConfigurationValue(dbContext, this.Scope, Key, Value);
}
private ValueType GetValue<ValueType>(string Key, ValueType Default)
{
return ConfigurationCache.GetConfigurationValue(dbContext, this.Scope, Key, Default);
}
protected void SetValue<ValueType>(string Key, ValueType Value)
protected void Set<ValueType>(ValueType Value, [CallerMemberName] string Key = null)
{
this.Context.SetConfigurationValue(this.Scope, Key, Value);
if (Key == null)
throw new ArgumentNullException("Key");
this.SetValue(Key, Value);
}
protected ValueType GetValue<ValueType>(string Key, ValueType Default)
protected ValueType Get<ValueType>(ValueType Default, [CallerMemberName] string Key = null)
{
return this.Context.GetConfigurationValue(this.Scope, Key, Default);
if (Key == null)
throw new ArgumentNullException("Key");
return this.GetValue(Key, Default);
}
protected void SetObsfucated(string Value, [CallerMemberName] string Key = null)
{
this.Set(ConfigurationCache.ObsfucateValue(Value), Key);
}
protected string GetDeobsfucated(string Default, [CallerMemberName] string Key = null)
{
var obsfucatedValue = this.Get<string>(null, Key);
if (obsfucatedValue == null)
return Default;
else
return ConfigurationCache.DeobsfucateValue(obsfucatedValue);
}
protected void SetAsJson<ValueType>(ValueType Value, [CallerMemberName] string Key = null)
{
if (Value == null)
this.Set<string>(null, Key);
else
this.Set(JsonConvert.SerializeObject(Value), Key);
}
protected ValueType GetFromJson<ValueType>(ValueType Default, [CallerMemberName] string Key = null)
{
var jsonValue = this.Get<string>(null, Key);
if (jsonValue == null)
return Default;
else
return JsonConvert.DeserializeObject<ValueType>(jsonValue);
}
}