0

i documented myself but couldn't found or understood how can i do what i want to achieve.

here is the thing :

  • I cannot use registry (not sure i have rights on client computer)
  • it need to be generic and transparent (code-side)

i have configuration settings in a project folder, let's says :

  • Solution
    • project1
      • many code folder..
      • Settings
        • UserLevel
          • FirstEnv.settings
          • SecondEnv.settings
        • ApplicationLevel
          • FirstEnv.settings
          • SecondEnv.settings
    • project2

now I have a SettingFactory which (should) instantiate (or a least, assign value) in *.settings to my applicationSettings i want to have something like :

public static void LoadSetting()
    {

        var env = ConfigurationManager.AppSettings["environnement"];
        switch (env)
        {
            case "env1":                    ConfigurationManager.OpenExeConfiguration("Setting/AppLevel/firstEnv.settings");
                break;
            case "env2":                    ConfigurationManager.OpenExeConfiguration("Setting/AppLevel/secondEnv.settings");
                break;
            default :                    ConfigurationManager.OpenExeConfiguration("Setting/AppLevel/secondEnv.settings");
                break;
        }
    }

and a '(key,value)' in app.config

<applicationSettings>
  <add name="environnement" value="env1"/>
</applicationSettings>

So in my code, when i need to use settings i can simply refer to

ConfigurationManager.appsettings["targetURI"]

and when i want to switch 'environnement'

ConfigurationManager.appsettings["environnement"] = "env2";
SettingsFactory.LoadSettings();

Any advice on how to do this or any design pattern improvement ?

Moreover, i separate AppLevel settings and Userlevel settings, but it's kinda nothing more than a pair of (key,value). But easier for dev to work with. This way, i can modify environnement with ease by Code ( 2lines) or by editing app.config

Thanks,

MinionKing
  • 187
  • 1
  • 4
  • 20

1 Answers1

0

I suggest you create your own class to store the settings that will be unique to each environment and then serialize/deserialize them to their own files using XmlSerialier.

Here's a really simple example of how XmlSerializer is used.

http://tech.pro/tutorial/798/csharp-tutorial-xml-serialization

static List<YourSettingsClass> DeserializeFromXML(string path)
{
    XmlSerializer deserializer = new XmlSerializer(typeof(List<YourSettingsClass>));
    TextReader textReader = new StreamReader(path);
    List<YourSettingsClass> settings;
    settings = (List<YourSettingsClass>)deserializer.Deserialize(textReader);
    textReader.Close();

    return settings;
}

Then store the path to your various environment settings files in your app.config.

<appSettings>
    <add key="env1" value="C:\temp\env1-settings.xml"/>
    <add key="env2" value="C:\temp\env2-settings.xml"/>
</appSettings>

Load them with something like this:

List<YourSettingsClass> envSettings = DeserializeFromXML(ConfigurationManager.AppSettings["env2"]);
FodderZone
  • 863
  • 12
  • 27