Database Enum – Keeping an Enum and a Table in Sync

databaseenum

I'm making a program that will post data to a database, and I've run into a pattern that I'm sure is familiar: A short table of most-likely (very strongly likely) fixed values that serve as an enum. So suppose the following table called Status:

  Status
  Id Description
  --------------
   0 Unprocessed
   1 Pending
   2 Processed
   3 Error

In my program I need to determine a status Id for another table, or possibly update a record with a new status Id.

I could hardcode the status Id's in an enum and hope no one ever changes the database. Or I could pre-fetch the values based on the description (thus hardcoding that instead).

What would be the correct approach to keep these two, enum and table, synced?

Best Answer

I would hard-code the enum within your program, as I suspect these different statuses affect your programs logic. If you have a new status, how is your program supposed to react to this new status?

Because the database is part of your application, I don't think it would make sense to affect one without consulting other, sort of speak.

Related Topic