Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConfigurationSettings vs Properties.Settings

I have a Winform app that has 16 SQL Connections currently stored in the DAL Projects Settings.settings.

I am trying to write a "Manager" class to simplify this(like here). However, most of the examples I find on the web seem to use ConfigurationSettings.AppSettings["something"]. While, I WAS using Properties.Settings.Default.something.

Can someone, please, explain which is considered CORRECT and why for a Desktop applications?

like image 481
Refracted Paladin Avatar asked Dec 28 '22 05:12

Refracted Paladin


2 Answers

I think the correct way is to use the app.config file and store them in the connectionStrings section?

Then you can access them like:

 ConfigurationManager.ConnectionStrings["something"].ConnectionString

You could write a wrapper around that easily if that is too "ugly".

public class ConnectionManager
{
    public string Something
    {
        get
        {
             // TODO: check to make sure the configuration exists and throw an exception perhaps
             return ConfigurationManager.ConnectionStrings["something"].ConnectionString;
        }
    }
}

Oops...as pointed out I meant to do ConfigurationManager and not ConfigurationSettings.

like image 177
Dismissile Avatar answered Jan 12 '23 01:01

Dismissile


We prefer to use Properties.Settings (aka. settings.settings) because it's strongly typed. Don't try to do fancy tricks that have different environments (dev/prod) in the same config file. It is much easier to have a separate config file per environment, and to rename the configs when you deploy them. i.e.

app.config 
app.stage.config
app.test.config
app.prod.config

We use PowerShell scripts (batch files on steroids) to do our deployments. I'll simplify what we do as a traditional batch file - (pseudo code/untested sample):

@echo off
:: %1 is the environment name (first parameter)
setlocal
set source=c:\tfs\project\bin\release\
set target=\\server\share\path\

xcopy /s/e %source% %target%

:: Using a copy command to rename/overwrite the env specific version
if exists %target%\app.%1.config copy /y %target%\app.%1.config %target%\app.config
like image 43
chilltemp Avatar answered Jan 11 '23 23:01

chilltemp