Enumeration practicality

M

Mark

Assume you have an enumeration like PhoneType { Home, Business, Cell }.
This enumeration corresponds with a lookup/dictionary table in your database
like:

phone_cd | phone_descr
1 Home
2 Business
3 Cell

Now, I could associated each enumeration value with the phone_cd used in the
database. However, that strikes me as problematic ... it's like having two
copies of the same information in different places. However, otherwise I
need to be able translate between my enumeration values, and the database
values.

Is there a best practice(s) for dealing with this situation? Obviously it
depends on the circumstances, but general comments would be appreciated.
Thanks in advance.

Mark
 
N

Nicholas Paldino [.NET/C# MVP]

Mark,

I can't see why you would need it in more than one place. If you had a
number of details that you wanted to store for each id, then I would
recommend storing it in the database (with a column for each property you
want to store).

Either that, or I would use an enumeration in code, and attach
attributes to the enumeration which have the values that you want to store.
You can get the values from these attributes through reflection.

Hope this helps.
 
M

Mark

Thanks Nicholas,

Are you suggesting that if I have a critical/obvious need to store the
code/description in the database, that I should likely only use the codes
(1, 2, 3) in the application rather than creating a corresponding
enumeration in my .NET code?

Thanks again.

Mark


Nicholas Paldino said:
Mark,

I can't see why you would need it in more than one place. If you had a
number of details that you wanted to store for each id, then I would
recommend storing it in the database (with a column for each property you
want to store).

Either that, or I would use an enumeration in code, and attach
attributes to the enumeration which have the values that you want to store.
You can get the values from these attributes through reflection.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Mark said:
Assume you have an enumeration like PhoneType { Home, Business, Cell }.
This enumeration corresponds with a lookup/dictionary table in your
database
like:

phone_cd | phone_descr
1 Home
2 Business
3 Cell

Now, I could associated each enumeration value with the phone_cd used in
the
database. However, that strikes me as problematic ... it's like having
two
copies of the same information in different places. However, otherwise
I
need to be able translate between my enumeration values, and the database
values.

Is there a best practice(s) for dealing with this situation? Obviously it
depends on the circumstances, but general comments would be appreciated.
Thanks in advance.

Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top