我不喜欢.NET Framework中的标准配置机制。 (ConfgurationManager,ConfigurationSection等)。
我希望能够更简单地管理我的应用程序配置文件。
例如,我想在我的应用程序文件夹中创建一个名为“Settings”的文件夹。有几个配置文件命名。
请看一下:
Settings:
- smtp.config
- database.config
- something.config
假设“smtp.config”文件具有以下简单结构:
<smtp>
<username>something</username>
<hostname>something</hostname>
...
</smtp>
我想创建以下类:
public class MySmtpSettings : SomeBaseClassFromSomeConfigLibrary
{
// May be some simple attributes here
public string username;
// May be some simple attributes here. For example:
// Like XPath: [ConfigAttr("smtp\username")], or simply: ConfigAttr("username")
public string hostname;
...
// Only code containing properties declaration.
}
我想使用我的设置对象:
var settings = new MySmtpSettings("smtp.config");
var hostname = settings.Hostname;
我不想使用ConfigurationSection类。看起来很难。
你知道我在哪里可以找到一个可扩展的,简单的开源库吗?
UPD。
@jjrdk:感谢您的回答,但是使用ConfigurationSection类我通常会创建下一个代码:
public class MyConfigurationSection : ConfigurationSection
{
[ConfigurationProperty("username")]
public ConfigurationTextElement<string> Username
{
get { return (ConfigurationTextElement<string>)this["username"]; }
set { this["username"] = value; }
}
// ...
{
public class ConfigurationTextElement<T> : ConfigurationElement
{
private T _value;
protected override void DeserializeElement(XmlReader reader, bool serializeCollectionKey)
{
_value = (T)reader.ReadElementContentAs(typeof(T), null);
}
public T Value
{
get { return _value; }
}
}
看起来并不简单。也许我做了一些我不理解的事情?
答案 0 :(得分:3)
如果您不想使用ConfigurationManager,可能需要查看一些对象序列化程序。
你也可以使用众多Dependency Injection (DI) / Inversion of Control (IoC)框架中的一个...但如果你不喜欢ConfigurationManager背后的复杂性,我相信你会发现DI更不具吸引力。< / p>
作为附注,您甚至可以使用LINQ-to-XML或.Net Framework中的许多其他XML / Text读者之一。甚至是好的INI文件。
答案 1 :(得分:0)
我使用了一段代码,我不知道它从何处开始,但使用Google代码搜索我设法在此处找到它:http://m3dafort2d.googlecode.com/svn/trunk/projects/NCode/NCode/Configuration/
然后我就这样使用它:
public class AppSettings : DictionaryConvertible
{
public AppSettings() {
// For unit testing
}
public AppSettings(ISettingsProvider settingsProvider)
: base(settingsProvider) {}
public string MyStringSetting { get; set; }
public int MyIntSetting { get; set; }
public bool MyBooleanSetting { get; set; }
}
这是在IoC场景中使用的设置,但您可以找到以正常方式使用它的方法。
我通常只在IoC容器中配置ISettingsProvider,然后我也配置AppSettings类。然后我可以将该类注入任何需要设置的组件中。如果我想将它们分组到逻辑集合中,我还可以将配置设置类拆分为几个类。
如果要读取与web.config中的AppSettings不同的设置,只需实现另一个ISettingsProvider。我曾经为SQL Server创建了一个。
答案 2 :(得分:0)
编写ConfigurationSectionHandler并不难;它只是遍历XML中的节点,当ConfigurationManager调用您的代码时,这些节点作为参数提供给您。您编写了一个方法(可能还有一些帮助程序),它返回您希望配置的数据结构。对于简单的XML,该方法也可以非常简单。请参阅http://msdn.microsoft.com/en-us/library/ms228056.aspx上的示例。
public class MyHandler : IConfigurationSectionHandler
{
#region IConfigurationSectionHandler Members
public object Create(object parent, object configContext, XmlNode section)
{
var config = new MailConfiguration();
foreach (XmlAttribute attribute in section.GetAttributes())
{
switch (attribute.Name)
{
case "server":
config.Server = attribute.Value;
break;
...
}
}
foreach (XmlNode node in section.ChildNodes)
{
switch (node.Name)
{
case "server":
config.Server = node.Value;
break;
...
}
}
}
return config;
#endregion
}
}
用作
var config = ConfigurationManager.GetSection("smtp") as MailConfiguration;