开发者

Where can I find some open source code to read/write configuration files?

I don't like standard mechanism of configuration in the .NET Framework. (ConfgurationManager, ConfigurationSection and other).

I want a simpler ability to manage my application configuration files.

For example, I want to create a folder named "Settings" in my application folder. There are several config files named.

Please, take a look:

Settings:
- smtp.config
- database.config
- something.config

Assume, the "smtp.config" file has the following simple stucture:

<smtp>
  <username>something</username>
  <hostname>something</hostname>
  ...
</smtp>

And I want to create the following class:

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.
}

And I want to use my setting object like:

var settings = new MySmtpSettings("smtp.config");
var hostname = settings.Hostname;

I don't want to use the ConfigurationSection class. It looks very hard.

Do you know where I can find an ext开发者_如何学Pythonensible, simple open source library for this?

UPD.

@jjrdk: Thank you for your answer but using the ConfigurationSection class I usually create the next code:

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; }
    }
}

It does not look simple. Maybe I do something I do not understand?


If you don't want to use the ConfigurationManager you might want to look at a few of the object serializers.

  • XmlSerializer
  • DataContractSerializer

You could also use one of the many Dependency Injection (DI)/Inversion of Control (IoC) frameworks... but if you don't like the complexity behind ConfigurationManager I'm sure you fill find DI even less appealing.

As a side note could you even use LINQ-to-XML or one of the many other XML/Text readers within the .Net Framework. Even good-ole INI files.


I have used a piece of code that I don't know where it came from in the beginning, but using Google Code Search I managed to find it here: http://m3dafort2d.googlecode.com/svn/trunk/projects/NCode/NCode/Configuration/

I then use it like this:

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; }
}

This is setup to be used in a IoC scenario, but you can probably find a way to use it in a normal way.

I usually just configure the ISettingsProvider in the IoC container and then I configure the AppSettings class as well. I can then just inject that class into any component that need the settings. I can also split the configuration settings classes into several class if I want to group them into logical collections.

If you want to read settings for somewhere different than the AppSettings in web.config, simply implement another ISettingsProvider. I created one for SQL Server at one point.


Writing a ConfigurationSectionHandler is not hard; it's simply a matter of traversing the nodes in the XML that's given to you as a parameter when the ConfigurationManager calls your code. You write one method (and probably a few helpers) that returns the data structure that you want for your configuration. For simple XML, the method can be quite simple as well. See the samples at 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
  }
}

Used as

var config = ConfigurationManager.GetSection("smtp") as MailConfiguration;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜