Where to store system values

This seems to be a general question, but I cannot find it. Perhaps "system meaning" is the wrong phrase.

Anyway, by "system value" I mean the built-in values ​​that the system has for a given concept. For example, if I have a list of categories (like Mexican, American, Italian, etc.), where would you save them? Could you copy them (perhaps list them) or put them in the database?

If you say hardcoded, will your answer change if users are able to create new categories? Obviously, you will need to store the new ones in a database (or some other medium like xml), but will you leave the default system values ​​hard-coded and then concatenate them at runtime?

+2


source to share


6 answers


There are some symbolic values ​​that are true constants for the application (such as the pi value or the maximum value an integer can represent). Putting them into source code is fine.

If you foresee that the user needs to change the values, you need to put them in an external configuration (or database) file. The presence of some values ​​in the code, and some in the configuration are confusing; for lists, it can also be difficult to get the user to delete values ​​they don't want.



For values ​​that are in the config file, you still want to keep them under source control. The following options are available for this:

  • for the config file, enter the default config file in the source control.
  • for SQL data, either have a file with SQL commands, or a program that creates the original database under the control of the source.
+2


source


If users can add, edit, or remove values ​​from the list, then obviously hardcoding them in the application is not a viable alternative. However, if the users are not in control of the list, and if the list is relatively short, then hard-coding them (like enumeration) is a perfectly valid choice, even if they must change quite often, if you have a mechanism to easily update the client application to the latest version. This approach is obviously easier than storing them in an external data source.



However, I can't think of too many uses for something like a list of nationalities other than a select list for data intended to work in a database, in which case it makes sense to pull the list from a table in the database that is bound to other tables using the appropriate relational constraints. I inherited several applications that maintained a list of options in two places: a database table and a corresponding enum compiled into the application. This situation is known as "not good".

+1


source


You answered your own question, I think.

If something never changes during the life of your project (good luck with that), I use an array or hash. I usually have a function that returns it.

But usually I end up saving anything that can be changed in the database.

If you want to distinguish the original from the user-submitted categories, add a column with a default value of 0 and don't set it to insert. Set the initial values ​​to 1.

0


source


If a list of values ​​is closely related to the logic of the application, I usually have no problem storing them with the application as an enumeration or similar. You will have to update your application with the new logic, and storing values ​​in the database just increases the chances that someone will mistakenly add or remove a value without updating that logic.

The example you gave ("Mexican", "American", "Italian") is nevertheless a prime candidate for database storage. This list is likely to change frequently and nothing needs to be updated in the application code to handle the new values. If you decide to handle Elbonian food, all you have to do is update the database and you're done. There is no reason to force an app update if you don't need to.

0


source


Other people gave good answers. I just want to add that the example you give to nationalities is a case where you shouldn't encode them as ENUMs in the database, because they can change with a high probability.

Use ENUM only for a small set of values ​​that are guaranteed not to change. That is, it would be logically pointless to add or remove values ​​from the set. If your set of values ​​can be changed, store them in a lookup table so that you can change data values ​​simply with UPDATE / INSERT / DELETE commands. Overriding ENUM is a metadata change and is usually expensive.

Note that if you think that the list of values ​​"probably" will not change, or there is "no reason" to change them, or some other such qualified rule, you can assume that some manager will require the set to change by some point.

PS: ENUM is a MySQL specific database type. Several other databases can achieve a similar result with the CHECK constraint.

0


source


Put your data into the database. Hardcode nothing.

These values ​​will be changed or at least expanded. If they are data related in any way, do yourself a favor and put them in the database to begin with. This not only saves you time later when adding additional values, but also allows them to participate in referential integrity constraints at the database level.

I smiled when I saw the title of your post because my databases almost always include the SystemValues ​​table, which is used to store "one-time" values ​​that customize the program's behavior, such as (for example) the root folder where the generated output will be stored. To get them, you also need the corresponding GetSystemValue (Key, ValueIfNull) stored procedure.

For projects that allow custom level customization, I also include a table named UserValues ​​with an additional UserID column and a GetUserValue (Key, UserID, ValueIfNull) stored procedure.

For those non-database-centric applications, Windows provides you with a "free" database — the registry — or you can build your own configuration files.

0


source







All Articles