In my previous blog entry, I had recommended storing
enums using their string representations when writing them to a database. There’s a caveat there though – the
enum names should be guaranteed to remain stable, because if you change the name of an
enum member, it breaks the database. For my specific scenario I ended up using
enum values with explicit
short values, so if the names ever change in future the
short values remain the same and thus the database remains consistent.
There are other approaches to solving this too. Reader Arnaud Weil suggests having a table for each
enum and the
enum id from this table is used to refer to the
enums elsewhere in the database. The guys at my current project have a specialized class that emulates
enums and also allows the ability to associate meta-information to each
enum value (like an image or a description). A third approach would be to use custom attributes and a type descriptor that’d extract the meta-information from an
enum type. The old adage is true – there are many ways to skin a cat.