내장을 사용했다 - in .NET Fx의 구성 클래스. 아래 코드가 C# 인 경우에도 거의 어려움없이 VB.NET으로 변환 할 수 있어야합니다 (또는 편집 및 컴파일 프로젝트에서 참조 할 수있는 어셈블리로 가져옵니다).
약간의 어려움없이 ConfigurationElementCollection 클래스를 키/값 쌍 사전으로 변환 할 수 있습니다 (값 쌍에 반사를 사용해야하거나 값 쌍으로 저장할 클래스가 설정 클래스를 가질 수 있음) ConfigurationElement에서 생성자 인수로 상속).
<!-- web.config -->
<!-- ... -->
<configuration>
<configSections>
<section name="sharding" type="Domain.ShardingSection, Domain.Configuration" />
</configSections>
</configuration>
<!-- ... -->
<sharding>
<configurationMappings>
<add lastDigit="0" sqlMapFileName="Shard-0.SqlMap.config" />
<add lastDigit="1" sqlMapFileName="Shard-1.SqlMap.config" />
<add lastDigit="2" sqlMapFileName="Shard-2.SqlMap.config" />
<add lastDigit="3" sqlMapFileName="Shard-3.SqlMap.config" />
<add lastDigit="4" sqlMapFileName="Shard-4.SqlMap.config" />
<add lastDigit="5" sqlMapFileName="Shard-5.SqlMap.config" />
<add lastDigit="6" sqlMapFileName="Shard-6.SqlMap.config" />
<add lastDigit="7" sqlMapFileName="Shard-7.SqlMap.config" />
<add lastDigit="8" sqlMapFileName="Shard-8.SqlMap.config" />
<add lastDigit="9" sqlMapFileName="Shard-9.SqlMap.config" />
</configurationMappings>
</sharding>
을 그리고 구성 클래스에 의해 표현 :
// ConfigurationElement.cs
public class ConfigurationElement : System.Configuration.ConfigurationElement
{
protected T GetValue<T>(string key, T defaultValue)
{
var value = default(T);
if (base[key] != null)
{
var str = base[key].ToString();
try
{
if (!String.IsNullOrEmpty(str))
value = (T)Convert.ChangeType(str, typeof(T));
}
catch // use the default
{
}
}
return value;
}
}
// ConfigurationElementCollection.cs
public abstract class ConfigurationElementCollection<TElement,TKey> :
ConfigurationElementCollection,
IEnumerable<TElement> where TElement : System.Configuration.ConfigurationElement, new()
{
public TElement this[int index]
{
get { return (TElement)BaseGet(index); }
}
public TElement this[TKey key]
{
get { return (TElement)BaseGet(key); }
}
protected override System.Configuration.ConfigurationElement CreateNewElement()
{
return new TElement();
}
protected override object GetElementKey(System.Configuration.ConfigurationElement element)
{
return GetElementKey((TElement)element);
}
protected abstract TKey GetElementKey(TElement element);
public TKey[] GetAllKeys()
{
var keys = BaseGetAllKeys();
var ret = new TKey[keys.Length];
for (var i = 0; i < keys.Length; i++)
ret[i] = (TKey)keys[i];
// done
return ret;
}
public void Add(TElement element)
{
BaseAdd(element);
}
public void Remove(TElement element)
{
BaseRemove(element);
}
public void Clear()
{
BaseClear();
}
IEnumerator<TElement> IEnumerable<TElement>.GetEnumerator()
{
foreach (TElement element in this)
{
yield return element;
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
throw new System.NotImplementedException();
}
}
그리고 여기가 내가 우리의 시스템에서 우리의 샤딩 전략 위의 기본 클래스 코드를 사용하는 예입니다 (이름은 무고한을 보호하기 위해 변경) XML 인스턴스 위 :
// ShardElement.cs
public class ShardElement : ConfigurationElement
{
[ConfigurationProperty("lastDigit", IsKey=true, IsRequired=true)]
public int LastDigit
{
get { return (int)this["lastDigit"]; }
}
[ConfigurationProperty("sqlMapFileName", IsRequired=true)]
public string SqlMapFileName
{
get { return (string)this["sqlMapFileName"]; }
}
}
// ShardElementCollection.cs
public class ShardElementCollection : ConfigurationElementCollection<ShardElement, int>
{
protected override int GetElementKey(ShardElement element)
{
return element.LastDigit;
}
}
// ShardingSection.cs
public class ShardingSection : ConfigurationSection
{
public const string Name = "sharding";
[ConfigurationProperty("configurationMappings")]
public ShardingElementCollection ConfigurationMappings
{
get { return (ShardingElementCollection)base["configurationMappings"]; }
}
}
A *에서 그 사실이 아니다 IDictionary가이 작업을 처리 할 수 있으며, 구성 파일은 실행시 업데이트되는 경우가없는 파일의 .config 동안 응용 프로그램을 다시 시작하거나 AppPool을 재사용하여 새 값을 가져옵니다.