Enums or Tables?

2020-05-27 11:12发布

I am making this a community wiki, as I would appreciate people's approach and not necessarily an answer.

I am in the situation where I have a lot of lookup type data fields, that do not change. An example would be:

Yearly Salary
Option: 0 - 25K
Option: 25K - 100K
Option: 100K +

I would like to have these options easily available through an enum, but would also like to textual values available in the DB, as I will do reporting on the textual values and not a ID. Also, since they are static I do not want to be making calls to the DB.

I was thinking duplicating this in an enum and table, but would like to hear some alternate thoughts.

Thanks

标签: c# enums
9条回答
Bombasti
2楼-- · 2020-05-27 11:32

I use both. In Linq to SQL and EF, you just make the column property an enum type. In other frameworks you can usually map the column to an enum property somehow. You can still have an primary key table in the database containing valid enums.

You could also do this with a CHECK constraint in the database, but that tends to tie your data to your application - somebody looking at the database alone wouldn't necessarily know what each value means. Therefore I prefer the hybrid table/enum.

查看更多
小情绪 Triste *
3楼-- · 2020-05-27 11:37

I think an enum is a bad idea. Just given the type of data you show, it's subject to change. Better to have a data base table with ID/Min/Max/Description fields that you load when your app initializes.

查看更多
爷的心禁止访问
4楼-- · 2020-05-27 11:38

Use both enum(for code) and DB texts- for GUI presentation.

So, if you will always have 3 option use an enum LowSalary, MiddleSalary and HighSalary, store your texts in the DB and switch your texts in the GUI corresponding to your property enum value.

查看更多
劫难
5楼-- · 2020-05-27 11:40

Since C# doesn't allow Enums with string values, so I would suggest a struct with some static strings.

That way, you maintain some Intellisense, but without trying to shoehorn an Enum value on what is a string value in the database.

The other solution I would offer: remove the logic that depends on these values and move to table-based logic. (For instance, if each traunch has a different tax rate, add tax rate as a column in the database rather than a case {} in the code.).

查看更多
Melony?
6楼-- · 2020-05-27 11:41

Have a look at my suggestion here How to work with Enums in Entity Framework?

Essentially I use default values sql scripts for core lookup data, with ID's for FK references from other tables, and then I use a simple T4 template to generate my enums for c#. That way the Database is efficient, normalised and correctly constrained, and my c# entities don't have to deal with ID's (Magic numbers).

Its simple quick, easy, and does the job for me.

I use EF4, but you don't need to, could use this approach with whatever technology you use for your entities.

查看更多
够拽才男人
7楼-- · 2020-05-27 11:44

First make sure this data is really static. If anything changes, you will have to recompile and redeploy.

If the data is really static, I would go the enum route. You could create a YearlySalaryEnum holding all the values. For string representation I would use a Dictionary with string values and the YearlySalaryEnum as Key. The dictionary can be hold as a static instance in a static class. Usage would be along the lines of (C#):

string highSalary = StaticValues.Salaries[YearlySalaryEnum.High];
查看更多
登录 后发表回答