mardi 30 décembre 2014

Flexible and easy to use settings class


I am thinking about a way to create a flexible "settings class" in C#. The question is: How to provide a confortable way to provide default values to the settings. I want the programmer to access the default settings as easy as possible using intellisense. Therefore I encapsulated the default values in a subclass of the actual settings class (instead of defining them directly in the settings class).



public class ServerSettings
{
public ServerSettings(int port = ServerSettings.Default.Port,
string anotherSetting = ServerSettings.Default.AnotherSetting)
{
this.Port = port;
this.AnotherSetting = anotherSetting;
}

public int Port { get; set; }
public string AnotherSetting { get; set; }

public class Default
{
public const int Port = 7000;
public const string AnotherSetting = "abcd";
}
}


The programmer now is able to access the default values like this:



ServerSettings.Default.Port


And after typing ServerSettings.Default a list with all available default settings will show up. It's like a "multitype enum".


What do you think of this solution? Do you have any alternative ideas? Or some criticism?





Aucun commentaire:

Enregistrer un commentaire