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
@@ -1,4 +1,5 @@
using System;
using Disco.Data.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -7,7 +8,7 @@ namespace Disco.Data.Configuration.Modules
{
public class BootstrapperConfiguration : ConfigurationBase
{
public BootstrapperConfiguration(ConfigurationContext Context) : base(Context) { }
public BootstrapperConfiguration(DiscoDataContext dbContext) : base(dbContext) { }
public override string Scope
{
@@ -18,11 +19,11 @@ namespace Disco.Data.Configuration.Modules
{
get
{
return this.GetValue("MacSshUsername", "root");
return this.Get("root");
}
set
{
this.SetValue("MacSshUsername", value);
this.Set(value);
}
}
@@ -30,11 +31,11 @@ namespace Disco.Data.Configuration.Modules
{
get
{
return ConfigurationContext.DeobsfucateValue(this.GetValue("MacSshPassword", string.Empty));
return this.GetDeobsfucated(string.Empty);
}
set
{
this.SetValue("MacSshPassword", ConfigurationContext.ObsfucateValue(value));
this.SetObsfucated(value);
}
}
}
@@ -3,43 +3,21 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using Disco.Models.Repository;
using Disco.Data.Repository;
namespace Disco.Data.Configuration.Modules
{
public class DeviceProfilesConfiguration : ConfigurationBase
{
// Removed 2012-06-14 G# - Properties moved to DeviceProfile model & DB Migrated in DBv3.
//private Dictionary<int, DeviceProfileConfiguration> deviceProfileConfigurations;
public DeviceProfilesConfiguration(DiscoDataContext dbContext) : base(dbContext) { }
public DeviceProfilesConfiguration(ConfigurationContext Context)
: base(Context)
{
// Removed 2012-06-14 G# - Properties moved to DeviceProfile model & DB Migrated in DBv3.
//this.deviceProfileConfigurations = new Dictionary<int, DeviceProfileConfiguration>();
}
public override string Scope
{
get { return "DeviceProfiles"; }
}
// Removed 2012-06-14 G# - Properties moved to DeviceProfile model & DB Migrated in DBv3.
//public DeviceProfileConfiguration DeviceProfile(DeviceProfile Profile)
//{
// DeviceProfileConfiguration dpc = default(DeviceProfileConfiguration);
// if (!this.deviceProfileConfigurations.TryGetValue(Profile.Id, out dpc))
// {
// dpc = new DeviceProfileConfiguration(this.Context, Profile);
// this.deviceProfileConfigurations[Profile.Id] = dpc;
// }
// return dpc;
//}
public override string Scope { get { return "DeviceProfiles"; } }
public int DefaultDeviceProfileId
{
get
{
var v = this.GetValue("DefaultDeviceProfileId", 1);
var v = this.Get(1);
if (v > 0)
return v;
else
@@ -49,20 +27,22 @@ namespace Disco.Data.Configuration.Modules
{
if (value < 1)
throw new ArgumentOutOfRangeException("value", "Expected >= 1");
this.SetValue("DefaultDeviceProfileId", value);
this.Set(value);
}
}
public int DefaultAddDeviceOfflineDeviceProfileId
{
get
{
return this.GetValue("DefaultAddDeviceOfflineDeviceProfileId", 0);
return this.Get(0);
}
set
{
if (value < 0)
throw new ArgumentOutOfRangeException("value", "Expected >= 0");
this.SetValue("DefaultAddDeviceOfflineDeviceProfileId", value);
this.Set(value);
}
}
@@ -5,22 +5,20 @@ using System.Text;
using Disco.Models.BI.Config;
using Disco.Models.Repository;
using Newtonsoft.Json;
using Disco.Data.Repository;
namespace Disco.Data.Configuration.Modules
{
public class OrganisationAddressesConfiguration : ConfigurationBase
{
public OrganisationAddressesConfiguration(ConfigurationContext Context) : base(Context) { }
public OrganisationAddressesConfiguration(DiscoDataContext dbContext) : base(dbContext) { }
public override string Scope
{
get { return "OrganisationAddresses"; }
}
public override string Scope { get { return "OrganisationAddresses"; } }
public OrganisationAddress GetAddress(int Id)
{
var address = default(OrganisationAddress);
var addressString = this.GetValue<string>(Id.ToString(), null);
var addressString = this.Get<string>(null, Id.ToString());
if (addressString != null)
{
if (addressString.StartsWith("{"))
@@ -45,28 +43,23 @@ namespace Disco.Data.Configuration.Modules
string addressString = JsonConvert.SerializeObject(Address);
this.SetValue(Address.Id.ToString(), addressString); //Address.ToConfigurationEntry());
this.Set(addressString, Address.Id.ToString()); //Address.ToConfigurationEntry());
return Address;
}
public void RemoveAddress(int Id)
{
// Set Config Item to null = Remove Configuration Item
this.SetValue<string>(Id.ToString(), null);
this.Set<string>(null, Id.ToString());
}
public List<OrganisationAddress> Addresses
{
get
{
Dictionary<string, ConfigurationItem> configAddress = default(Dictionary<string, ConfigurationItem>);
if (this.Context.ConfigurationDictionary(this.Scope).TryGetValue(this.Scope, out configAddress))
return configAddress.Select(
ca => ca.Value.Value.StartsWith("{") ?
JsonConvert.DeserializeObject<OrganisationAddress>(ca.Value.Value) :
OrganisationAddress.FromConfigurationEntry(int.Parse(ca.Key), ca.Value.Value)
).ToList();
else
return new List<OrganisationAddress>(); // Empty List - No Addresses
return this.Items.Select(ca => ca.Value.StartsWith("{") ?
JsonConvert.DeserializeObject<OrganisationAddress>(ca.Value) :
OrganisationAddress.FromConfigurationEntry(int.Parse(ca.Key), ca.Value)
).ToList();
}
}
@@ -77,7 +70,7 @@ namespace Disco.Data.Configuration.Modules
int nextId = 0;
while (true)
{
if (this.Context.ConfigurationItem(this.Scope, nextId.ToString()) == null)
if (this.Get<string>(null, nextId.ToString()) == null)
break;
nextId++;
}
@@ -1,109 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Disco.Data.Configuration.Modules
{
public class WirelessConfiguration : ConfigurationBase
{
public const string Provider_eduSTAR = "eduSTAR";
public const string Provider_eduPaSS = "eduPaSS";
public WirelessConfiguration(ConfigurationContext Context) : base(Context) { }
public override string Scope
{
get { return "Wireless"; }
}
public int CertificateAutoBufferMax
{
get
{
return this.GetValue("CertificateAutoBufferMax", 50);
}
set
{
this.SetValue("CertificateAutoBufferMax", value);
}
}
public int CertificateAutoBufferLow
{
get
{
return this.GetValue("CertificateAutoBufferLow", 10);
}
set
{
this.SetValue("CertificateAutoBufferLow", value);
}
}
public string Provider
{
get
{
return this.GetValue("Provider", Provider_eduSTAR);
}
set
{
if (string.IsNullOrEmpty(value))
throw new ArgumentNullException("value");
if (value.Equals(Provider_eduSTAR, StringComparison.InvariantCultureIgnoreCase))
this.SetValue("Provider", Provider_eduSTAR);
else
throw new NotSupportedException(string.Format("Unsupported Wireless Provider: ", value));
}
}
#region eduSTAR Configuration
public string eduSTAR_Scope
{
get { return "Wireless_eduSTAR"; }
}
public string eduSTAR_ServiceAccountSchoolId
{
get
{
return this.Context.GetConfigurationValue<string>(this.eduSTAR_Scope, "ServiceAccountSchoolId", null);
}
set
{
if (string.IsNullOrEmpty(value))
throw new ArgumentNullException("value");
this.Context.SetConfigurationValue(this.eduSTAR_Scope, "ServiceAccountSchoolId", value);
}
}
public string eduSTAR_ServiceAccountUsername
{
get
{
return this.Context.GetConfigurationValue<string>(this.eduSTAR_Scope, "ServiceAccountUsername", null);
}
set
{
if (string.IsNullOrEmpty(value))
throw new ArgumentNullException("value");
this.Context.SetConfigurationValue(this.eduSTAR_Scope, "ServiceAccountUsername", value);
}
}
public string eduSTAR_ServiceAccountPassword
{
get
{
return ConfigurationContext.DeobsfucateValue(this.Context.GetConfigurationValue<string>(this.eduSTAR_Scope, "ServiceAccountPassword", null));
}
set
{
if (string.IsNullOrEmpty(value))
throw new ArgumentNullException("value");
this.Context.SetConfigurationValue(this.eduSTAR_Scope, "ServiceAccountPassword", ConfigurationContext.ObsfucateValue(value));
}
}
#endregion
}
}