Is a great habit to make use of enums with databases (NHibernate + mysql)? What exactly are the pros and cons? Is there influence to database efficiency?
Is a great habit to make use of enums with databases (NHibernate + mysql)?
It certainly is sensible from the programmer's perspective to make use of enums where relevant. It can make for a lot more readable code and removes "invalid" values inside your database.
What exactly are the pros and cons? Is there influence to database efficiency?
- Readability in the code
- Data integrity
- Overhead in conversions of enums to database types. Even though this is most likely minimal. Haven't examined this however i would assume this is actually the situation.
I am sure the relaxation from the community can certainly increase the list above however i would say overall using enums may be beneficial where it seems sensible.
I believe from the code perspective, enums certainly seem sensible. However, if you work with NHibernate to create your database schema, watch out for this using MySQL's Enum Data Type. This can store your enums as strings, but enforce that only valid values are joined. This is often pricey from the storage perspective, as well as when attempting to refactor. The enum database type will handle validation in the database level for you personally, so arbitrary updates is going to be less worrisome, I favor storing the number value and depending around the application to validate, since it causes it to be simpler to include or relabel enum people.
I've no clue if this sounds like a great way or otherwise (if you are using fluent nhibernate, this could probably be accomplished using conventions) but when you appear at this blog post you'll find an IUserType that handles mapping enums as integers in MySQL and covers a few of the challenges experienced.