Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielgreen/5772822 to your computer and use it in GitHub Desktop.
Save danielgreen/5772822 to your computer and use it in GitHub Desktop.
Code to create a custom configuration section with child collections
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="CLGSettings" type="Config.CLGSettings, MyAssembly" />
</configSections>
<CLGSettings defaultEnvironment="Training">
<Environments>
<Environment name="Training" webServiceURL="https://www.example.com/training">
<Accounts>
<Account username="TL-train" password="Password" serviceId="TL" serviceName="Argyll &amp; Bute" />
<Account username="TM-train" password="Password" serviceId="TM" serviceName="East &amp; West Dunbartonshire" />
<Account username="TN-train" password="Password" serviceId="TN" serviceName="North Lanarkshire" />
<Account username="TP-train" password="Password" serviceId="TP" serviceName="South Lanarkshire" />
</Accounts>
</Environment>
<Environment name="Live" webServiceURL="https://www.example.com/live">
<Accounts>
<Account username="TL-prod" password="Password" serviceId="TL" serviceName="Argyll &amp; Bute" />
<Account username="TM-prod" password="Password" serviceId="TM" serviceName="East &amp; West Dunbartonshire" />
<Account username="TN-prod" password="Password" serviceId="TN" serviceName="North Lanarkshire" />
<Account username="TP-prod" password="Password" serviceId="TP" serviceName="South Lanarkshire" />
</Accounts>
</Environment>
</Environments>
</CLGSettings>
</configuration>
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Config
{
public abstract class BaseConfigurationElementCollection<TKey, TElement>
: ConfigurationElementCollection, IEnumerable<TElement>
where TElement : ConfigurationElement, new()
{
protected override ConfigurationElement CreateNewElement()
{
return new TElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return GetElementKey((TElement)element);
}
protected abstract TKey GetElementKey(TElement element);
IEnumerator<TElement> IEnumerable<TElement>.GetEnumerator()
{
foreach (TElement element in this)
{
yield return element;
}
}
public virtual void Add(TElement configurationElement)
{
BaseAdd(configurationElement);
}
public virtual void Remove(TKey key)
{
BaseRemove(key);
}
protected virtual TElement Get(TKey key)
{
return (TElement)BaseGet(key);
}
protected virtual TElement Get(int index)
{
return (TElement)BaseGet(index);
}
public TElement this[TKey key]
{
get { return Get(key); }
}
public TElement this[int index]
{
get { return Get(index); }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Collections;
namespace Config
{
public class CLGSettings : ConfigurationSection
{
private static CLGSettings _settings = ConfigurationManager.GetSection("CLGSettings") as CLGSettings;
public static CLGSettings Settings
{
get
{
return _settings;
}
}
[ConfigurationProperty("defaultEnvironment", IsRequired = true)]
public string DefaultEnvironment
{
get { return (string)this["defaultEnvironment"]; }
set { this["defaultEnvironment"] = value; }
}
[ConfigurationProperty("Environments", IsRequired = true)]
[ConfigurationCollection(typeof(EnvironmentSettings), AddItemName = "Environment")]
public EnvironmentSettings Environments
{
get { return (EnvironmentSettings)this["Environments"]; }
set { this["Environments"] = value; }
}
}
public class EnvironmentSettings : BaseConfigurationElementCollection<string, EnvironmentSetting>
{
protected override string GetElementKey(EnvironmentSetting element)
{
return element.Name;
}
}
public class EnvironmentSetting : ConfigurationElement
{
[ConfigurationProperty("Accounts", IsRequired = true)]
[ConfigurationCollection(typeof(AccountSettings), AddItemName = "Account")]
public AccountSettings Accounts
{
get { return (AccountSettings)this["Accounts"]; }
set { this["Accounts"] = value; }
}
[ConfigurationProperty("name", IsKey = true, IsRequired = true)]
public string Name
{
get { return (string)this["name"]; }
set { this["name"] = value; }
}
[ConfigurationProperty("webServiceURL", IsRequired = true)]
public string WebServiceURL
{
get { return (string)this["webServiceURL"]; }
set { this["webServiceURL"] = value; }
}
}
public class AccountSettings : BaseConfigurationElementCollection<string, AccountSetting>
{
protected override string GetElementKey(AccountSetting element)
{
return element.Username;
}
}
public class AccountSetting : ConfigurationElement
{
[ConfigurationProperty("username", IsKey=true, IsRequired=true)]
public string Username
{
get { return (string)this["username"]; }
set { this["username"] = value; }
}
[ConfigurationProperty("password", IsRequired = true)]
public string Password
{
get { return (string)this["password"]; }
set { this["password"] = value; }
}
[ConfigurationProperty("serviceName", IsRequired = true)]
public string ServiceName
{
get { return (string)this["serviceName"]; }
set { this["serviceName"] = value; }
}
[ConfigurationProperty("serviceId", IsRequired = true)]
public string ServiceID
{
get { return (string)this["serviceId"]; }
set { this["serviceId"] = value; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment