views:

191

answers:

2

We have a smelly code in our project. A lot of values which used in biz logic have 2 places where they stored: we have dictionary tables (used in FK relations) in DB where we stored values e.x. MessageDirectionInfo:

0|Unknown

1|In

2|Out

and we have Enum with exactly same data: MessageDirectionEnum{Unknown=0,In=1,Out=2} And all over the code we have a lot of "switch" and "if" where enum used for conditional logic. But when we saving to DB - we should "convert" (which actually "blind select", case its selecting exact same data except result object type) Enum value to Dictionary class instance. I can understand that Enums is used to clear code logic and DB data used to reference in foreign keys, but actually i dont like this.

Is there any pattern to remove such behaviour or any way to clear some of this "smelly way" ? Or maybe I'm just blind and this is the only clear way to get both benefits - DB FKs and clearer code logic with enums

A: 

How about generating the enum source code directly from the reference tables in the Database?

Mitch Wheat
Enums is pretty short and simple so its not the problem to create enums, its problem that we have 2 sources of data - table with id field and enum with exactly same values. Enums generation will definitely solve problem with potential value divergence, but i want to abandon enums at all
Alexey Shcherbak
+1  A: 

Keep the table but replace the Enum with classes...? http://www.refactoring.com/catalog/replaceTypeCodeWithClass.html

gkrogers
Thanks for pointing on that pattern ( btw I started to read Kerievsky book 3 days ago, but not yet reached this pattern), but my current situation with Enum as value holder is not so different from Class replacement in that pattern. We already have compile time value checks with Enum nature.
Alexey Shcherbak
This pattern has never occurred to me before. Thanks for this link!
Daniel Auger