Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuration settings placement in 3-layered application

In a standard 3-layered application (Winforms Ui, BLL, DAL) i need to have settings specific for each layer.

Should i use the Winforms application settings to include all of them or i should have each dll (BLL,DAL) come with their settings?

I need a simple solution and not in a database or custom xmls, also i need to provide multiple UIs for the same architecture.

Update: Currently i am leaning towards separate .config files for each layer, i just dont know if this is the best practice that will allow for most of future changes (changes in layers and/or multiple UI apps).

Outcome: I think i am gonna have a static singleton class in every project that i need settings. I am gonna populate them by the upper layer every time with the most suitable way.

like image 760
e4rthdog Avatar asked Oct 19 '25 09:10

e4rthdog


2 Answers

  1. Custom xml file is flexible approach but need a bit effort.

  2. Use a separate library project only to serve for settings, its easier way as you may use default settings class to save/load settings but not very flexible for nested settings.

  3. Put all settings with DAL since it live at root and all other projects (UI, BAL) reference it)

like image 167
Munawar Avatar answered Oct 21 '25 23:10

Munawar


Every time I tried to use the built-in app.config file, I ended up implementing my own config solution due to shortcomings of the built-in solution. Implementing a custom xml-based solution is not complex. It is actually very easy.

Just put this base class into your solution:

[Serializable]
public abstract class ConfigBase<DerivedT> where DerivedT : ConfigBase<DerivedT>
{
    protected string FilePath;
    public string FileVersion;

    public ConfigBase() { }

    public void Save()
    {
        XmlSerializer xs = new XmlSerializer(GetType());
        using (StreamWriter writer = File.CreateText(FilePath))
        {
            xs.Serialize(writer, this);
        }
    }

    public static DerivedT Load(string filename)
    {
        XmlSerializer xs = new XmlSerializer(typeof(DerivedT));
        using (StreamReader reader = File.OpenText(filename))
        {
            DerivedT config = (DerivedT)xs.Deserialize(reader);
            config.FilePath = filename;
            return config;
        }
    }
}

Then you can make your configuration file like this:

public class Config : ConfigBase<Config>
{
    // put your variables here like below
    public string DatabaseConnectionString;
    public int numberOfConnections;
}

Use it like this:

// Load it like this
Config config = Config.Load(ConfigFileName);
// Save it like this
config.Save();

Feel free to use properties, arrays and other complex structures within the config file. It will all serialize automatically. Use XmlIgnore attribute if you do not want certain fields/properties serialized. With this solution you can have many different configuration files, but have a single mechanism to load and save them.

I often include a public static Config GenerateDefault(string ConfigFileName) factory method inside the Config file, which will produce a sample config with default values.

Don't forget to check if the file file exists and load it within a try/catch block.

An even better solution would be to use DataContracts, which allows you to serialize private members and provides good mechanisms for supporting different versions of DataContracts, but it is a bit more complex.

like image 39
Eiver Avatar answered Oct 21 '25 23:10

Eiver