Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to store application global settings?

I have some global settings For example some below are

ShortLeaveAllowedInOneDay = 2
LeaveAllowedInMonth = 3

I have following options to store these global settings

1-Store in Database table
2-Store in Webconfig file
3-Store in class as const filed
4-In XML file

Could you please suggest me which one is better approach and why

I am using Asp.net MVC3 with sqlserver 2005

like image 347
Ali Avatar asked Feb 23 '12 07:02

Ali


2 Answers

It depends on your requirements, each one of these options has it own advantages and disadvantages. It tried to list a few:

1. Store in Database table
Advantages:

  • Relatively easy to read settings.
  • Possible to write/update settings.
  • Access is to the database is fast.
  • Updates to database values are immediately available.
  • DB is can be shared across multiple instances in clustered environment.

Disadvantages:

  • More infrastructure required than the rest of the options (i.e. tables, db access etc).
  • If done incorrectly DB IO can become an issue. (Can be solved with caching strategies)



2. Store in web.config file
Advantages:

  • Simple to add and access settings.

Disadvantages:

  • Changes to the web.config may result in the application pool to restart.
  • Settings are generally not encrypted.
  • In a clustered environment the file has to be kept in sync with other instances.
  • Generally have to deal with strings data types and possible invalid user input when settings are set.



3. Store in class as const field
Advantages:

  • Very simple to work with.
  • Can work with static types.
  • Good first step towards refactoring settings into one of the other options.

Disadvantages:

  • Requires rebuild for settings to change.


4. In XML file
Advantages:

  • Convenient for storing complex settings such a hierarchies.
  • Custom XML config settings can be embedded inside the web.config. (Popular option see log4net as one such example)
  • Updates to the config files can be made without restarting the application pool.
  • An XSD can enforce the validity of the settings in the file (both structure and data types)

Disadvantages:

  • It is XML. Not really human readable, formats like YAML improves on that.
  • Implementation required to parse XML for reading and writing settings.
like image 153
Philip Fourie Avatar answered Sep 30 '22 23:09

Philip Fourie


If you need them to be configured by a user of your software I would not do option 3. If they are settings that you define as a programmer and do not expect them to be changed when your application is in production you could do that.

I would say that option 4 and 2 are basically the same, conceptually, and it is personal preference which to choose. Personally I like to define a custom configuration section and then have just that section defined in it's own .config file (this shows how to do that) so that you don't have a really massive web.config that the user has to navigate.

I would choose option 1 if I had a scenario where I had multiple components that all need access to the same configuration. If all you are building is a single web application, it does not feel necessary to me to do that but if, for example, you have a web application and some other client application and both require access to the database then storing the configuration there is a good choice.

like image 40
kmp Avatar answered Sep 30 '22 23:09

kmp