Where to store global application settings?

I have some global settings. For example, some of them are below.

ShortLeaveAllowedInOneDay = 2 LeaveAllowedInMonth = 3 

I have the following options for storing these global settings

1-Store in the database table
2-Store in Webconfig File
3-store in the class as const applied 4-In XML file

Could you suggest me which one is better suited and why

I am using Asp.net MVC3 with sqlserver 2005

+4
source share
7 answers

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

1. Store in a database table
Benefits:

  • Relatively easy to read settings.
  • It is possible to record / update settings.
  • Access to the database is fast.
  • Database value updates are immediately available.
  • The database can be divided into several instances in a clustered environment.

Disadvantages:

  • More infrastructure is required than other parameters (i.e. tables, db access, etc.).
  • If done incorrectly, there may be a problem with entering IO. (Can be solved using caching strategies)



2. Store in web.config file
Benefits:

  • Easy to add and access settings.

Disadvantages:

  • Changes to web.config may cause the application pool to restart.
  • Settings are usually not encrypted.
  • In a clustered environment, the file must be synchronized with other instances.
  • As a rule, you have to deal with data types of strings and possible invalid user inputs when setting parameters.



3. Save in the class as a const field
Benefits:

  • Very easy to work with.
  • It can work with static types.
  • A good first step to setting up refactoring is one of the other options.

Disadvantages:

  • Rebuild parameters to change.


4. In the XML file
Benefits:

  • Convenient for storing complex settings such as hierarchies.
  • Custom XML configuration settings can be built into web.config. (For a popular option, see log4net as one such example)
  • Configuration file updates can be performed without restarting the application pool.
  • XSD can ensure the correct settings in the file (structure and data types)

Disadvantages:

  • This is XML. In fact, it is not human readable, so formats like YAML are improving.
  • The implementation required for parsing XML for reading and writing.
+9
source

If you need them to be configured by the 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 change when your application is in production, you can do what.

I would say that option 4 and 2 are basically the same, conceptually, and this is a personal preference to choose. Personally, I like to define a user configuration section , and then it only has this section, its own .config file defined in it ( this shows how to do this) so that you do not have a really massive web.config that the user must execute.

I would choose option 1 if I had a script in which I had several components that needed access to the same configuration. If everything you build is a single web application, it does not seem necessary to me to do this, but if you have a web application and some other client application, and both require access to the database, then saving the configuration there is a good choice .

+1
source

Add the AppSettings session to the web configuration file, which can be accessed from the code, for example:

 System.Configuration.ConfigurationManager.AppSettings["ShortLeaveAllowedInOneDay "]; 

EDIT: and the Confir file will look like this:

 <appSettings> <add key="ShortLeaveAllowedInOneDay " value="2" /> </appSettings> 
+1
source

Saving global variables in the web.config file is a very common task. Storing values ​​in the web.config file is extremely useful when the database can be full and when you do not need a separate external file.

Reading an XML file several times for each page life cycle would be very bad for performance.

I would definitely go for web.config.

0
source

No approach is inherently better than others.

The best approach completely depends on your requirements for such things as security, scalability, flexibility, read-only accessibility and writeability, configuration configuration, etc.

0
source

You can save this variable in the Web.config file.

0
source

I will be more practical. Two main cases.

  • Values ​​/ parameters that change them and are critical for starting / running / initializing a program.
  • The values ​​are a change, or they are different for each user.

You save the source variables to web.config and everything else in the database. If you do not have a database, then what is available, for example, an XML file.

0
source

Source: https://habr.com/ru/post/1397968/


All Articles