Object-Oriented – Should You Create an Enum Mapping to a Database Table

enumobject-orientedrelational-database

I have a database table containing a list of systems relevant to the tool I am building, mostly in-house applications, or third-party systems we receive data from. This table is added to infrequently, approx every 2 months.

One of these systems is Windows itself, which is where we store our users' LANs, and I now need to explicitly reference the ID relating to Windows to query for user name, team etc. I know it would be bad practice to embed the ID itself into the code, so my question is what would be the best way to avoid this? I'm assuming my options are:

  • create a global constant representing this ID
  • create a global enum containing all systems now
  • create a global enum and add systems to it as & when they are required in the code
  • retrieve the ID from the database based on system name

I appreciate this may seem a trivial question, but I am going to have many situations like this during the course of this build, and although we are in complete control of the database I would like to conform to best practice as far as possible.

Many thanks!

Best Answer

Short Answer: i would keep my data in the database. The last option is good one.

retrieve the ID from the database based on system name

In order to deal gracefully with IDs that you are referring, you may use Dictionaries to load all reference Id's from database table(s) within your application. Depending on how frequently this Id's may change, you may also cach this dictionary for a much better performance.