<p>I'm really confused by the various configuration options for .Net configuration of dll's, ASP.net websites etc in .Net v2 - especially when considering the impact of a config file at the UI / end-user end of the chain.</p>

<p>So, for example, some of the applications I work with use settings which we access with:</p>

<pre><code>string blah = AppLib.Properties.Settings.Default.TemplatePath;
</code></pre>

<p>Now, this option seems cool because the members are stongly typed, and I won't be able to type in a property name that doesn't exist in the Visual Studio 2005 IDE. We end up with lines like this in the App.Config of a command-line executable project:</p>

<pre><code>&lt;connectionStrings&gt;
    &lt;add name="AppConnectionString" connectionString="XXXX" /&gt;
    &lt;add name="AppLib.Properties.Settings.AppConnectionString" connectionString="XXXX" /&gt;
&lt;/connectionStrings&gt;
</code></pre>

<p>(If we don't have the second setting, someone releasing a debug dll to the live box could have built with the debug connection string embedded in it - eek)</p>

<p>We also have settings accessed like this:</p>

<pre><code>string blah = System.Configuration.ConfigurationManager.AppSettings["TemplatePath_PDF"];
</code></pre>

<p>Now, these seem cool because we can access the setting from the dll code, or the exe / aspx code, and all we need in the Web or App.config is:</p>

<pre><code>&lt;appSettings&gt;
   &lt;add key="TemplatePath_PDF" value="xxx"/&gt;
&lt;/appSettings&gt;
</code></pre>

<p>However, the value of course may not be set in the config files, or the string name may be mistyped, and so we have a different set of problems.</p>

<p>So... if my understanding is correct, the former methods give strong typing but bad sharing of values between the dll and other projects. The latter provides better sharing, but weaker typing.</p>

<p>I feel like I must be missing something. For the moment, I'm not even concerned with the application being able to write-back values to the configuration files, encryption or anything like that. Also, I had decided that the best way to store any non-connection strings was in the DB... and then the very next thing that I have to do is store phone numbers to text people in case of DB connection issues, so they must be stored outside the DB!</p>

[{'answer_id': 57953, 'author': 'GEOCHET', 'author_id': 5640, 'author_profile': 'https://Stackoverflow.com/users/5640', 'pm_score': 0, 'selected': False, 'text': '<p>I think your confusion comes from the fact that it looks like your first example is a home-brewed library, not part of .NET.\nThe configurationmanager example is an example of built-in functionality.</p>\n'}, {'answer_id': 57980, 'author': 'Rob Gray', 'author_id': 5691, 'author_profile': 'https://Stackoverflow.com/users/5691', 'pm_score': 2, 'selected': False, 'text': "<p>If you use the settings tab in VS 2005+, you can add strongly typed settings and get intellisense, such as in your first example.</p>\n\n<pre><code>string phoneNum = Properties.Settings.Default.EmergencyPhoneNumber;\n</code></pre>\n\n<p>This is physically stored in App.Config.</p>\n\n<p>You could still use the config file's appSettings element, or even roll your own ConfigurationElementCollection, ConfigurationElement, and ConfigurationSection subclasses.</p>\n\n<p>As to where to store your settings, database or config file, in the case of non-connection strings:  It depends on your application architecture.  If you've got an application server that is shared by all the clients, use the aforementioned method, in App.Config on the app server.  Otherwise, you may have to use a database.  Placing it in the App.Config on each client will cause versioning/deployment headaches.</p>\n"}, {'answer_id': 57993, 'author': 'Dr8k', 'author_id': 6014, 'author_profile': 'https://Stackoverflow.com/users/6014', 'pm_score': 0, 'selected': False, 'text': "<p>I support Rob Grays answer, but wanted to add to it slightly. This may be overly obvious, but if you are using multiple clients, the app.config should store all settings that are installation specific and the database should store pretty much everything else.</p>\n\n<p>Single client (or server) apps are somewhat different. Here it is more personal choice really. A noticable exception would be if the setting is the ID of a record in the database, in which case I would always store the setting <em>in</em> the database with a foreign key to ensure the reference doesn't get deleted.</p>\n"}, {'answer_id': 58020, 'author': 'Nij', 'author_id': 6004, 'author_profile': 'https://Stackoverflow.com/users/6004', 'pm_score': 0, 'selected': False, 'text': "<p>Yes - I think I / we are in the headache situation Rob descibes - we have something like 5 or 6 different web-sites and applications across three independent servers that need to access the same DB. As things stand, each one has its own Web or App.config with the settings described setting and / or overriding settings in our main DB-access dll library.</p>\n\n<p>Rob - when you say application server, I'm not sure what you mean? The nearest thing I can think is that we could at least share some settings between sites on the same machine by putting them in a web.config higher in the directory hierarchy... but this too is not something I've been able to investigate... having thought it more important to understand which of the strong or weak-typed routes is 'better'.</p>\n"}, {'answer_id': 58206, 'author': 'Rob Gray', 'author_id': 5691, 'author_profile': 'https://Stackoverflow.com/users/5691', 'pm_score': 3, 'selected': True, 'text': '<p>Nij, our difference in thinking comes from our different perspectives. I\'m  thinking about developing enterprise apps that predominantly use WinForms clients.  In this instance the business logic is contained on an application server.  Each client would need to know the phone number to dial, but placing it in the App.config of each client poses a problem if that phone number changes.  In that case it seems obvious to store application configuration information (or application wide settings) in a database and have each client read the settings from there.  </p>\n\n<p>The other, .NET way, (I make the distinction because we have, in the pre .NET days, stored application settings in DB tables) is to store application settings in the app.config file and access via way of the generated Settings class.</p>\n\n<p>I digress.  Your situation sounds different.  If all different apps are on the same server, you could place the settings in a web.config at a higher level.  However if they are not, you could also have a seperate "configuration service" that all three applications talk to get their shared settings.  At least in this solution you\'re not replicating the code in three places, raising the potential of maintenance problems when adding settings.  Sounds a bit over engineered though.</p>\n\n<p>My personal preference is to use strong typed settings.  I actually generate my own strongly typed settings class based on what it\'s my settings table in the database.  That way I can have the best of both worlds.  Intellisense to my settings and settings stored in the db (note: that\'s in the case where there\'s no app server).</p>\n\n<p>I\'m interested in learning other peoples strategies for this too :)</p>\n'}]
['https://Stackoverflow.com/questions/57947', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/6004/']