简体   繁体   English

最佳实践:使用枚举或不存储下拉值

[英]Best practice : use enum or not to store dropdown values

I'm developping an application and I often ask myself the same question. 我正在开发一个应用程序,我经常问自己同样的问题。

For example, I have many types of user and in a form used to create a user, there is a dropdown to specify which type of user. 例如,我有许多类型的用户,并且在用于创建用户的表单中,有一个下拉列表,用于指定哪种类型的用户。

The best way to populate this dropdown is to store values in a database's table ? 填充此下拉列表的最佳方法是将值存储在数据库的表中? If I do this, when I develop I want to test type of user and I have only an int. 如果我这样做,当我开发时,我想测试用户类型,我只有一个int。 But I thing it's a better practice to test with enum. 但我认为使用枚举进行测试是一种更好的做法。 So I create enum but I feel it's a bad practice because I have to keep database and enum synchronized. 所以我创建枚举,但我觉得这是一个不好的做法,因为我必须保持数据库和枚举同步。

Another question is about localization. 另一个问题是关于本地化。 If I put values in database, I can't use resource file. 如果我把值放在数据库中,我就不能使用资源文件了。

Could you tell me good pratices about this ? 你能告诉我这方面的好心情吗?

Thanks 谢谢

in your situation - the database would be the best practice here especially if its dynamic data. 在你的情况下 - 数据库将是这里的最佳实践,特别是如果它的动态数据。 enum is for those values which are rarely to change, maybe once in a while but not on a frequent basis. 枚举是针对那些很少改变的值,可能是偶尔但不是经常发生的。 you may have new entries entered in the database regularly especially for things like cascading drop down lists. 您可能会定期在数据库中输入新条目,尤其是级联下拉列表等内容。

database certainly is the way to go in your situation. 数据库肯定是你的情况的方式。 Enums are there for those times where they are just a set standard and rarely to change, for example: 枚举存在于那些只是一个标准并且很少改变的时候,例如:

Mr. Miss. Mrs. Ms. Dr. 小姐先生。太太女士博士

you would have these in enums as they will never really change. 你会在枚举中有这些,因为它们永远不会真正改变。 on the other hands if store departments are to be changed or renamed, database would be the place to store such entries. 另一方面,如果要更改或重命名商店部门,数据库将是存储此类条目的地方。

I strongly disagree with using enums for this kind of functionality, for basically two reasons: 我强烈不同意使用枚举这种功能,基本上有两个原因:

  • Enumeration values have no behaviour, so compromise good OOP. 枚举值没有行为,因此妥协良好的OOP。 A good class has data + behaviour, so enumeration members are not sufficiently specialised to represent the concept they are named for. 一个好的类具有数据+行为,因此枚举成员不足以专门表示它们的命名概念。 The logic concerning this domain object lives somewhere else other than the entity that bears its name, which I dislike. 关于这个域对象的逻辑生活在除了其名称的实体之外的其他地方,我不喜欢。
  • Enumerations are meant to convey ordinality, so DaysOfWeek is a good usage (except that which day of the week is 'first' varies depending on culture, but that' nitpicking) because the enumeration denotes the order of its members. 枚举是为了传达正常性,所以DaysOfWeek是一个很好的用法(除了一周中的哪一天是'第一'取决于文化,但是'挑剔),因为枚举表示其成员的顺序 In your case, does it make sense to say that a particular value is the 'first' user type, the second value is second, and so on? 在您的情况下,说某个特定值是“第一个”用户类型,第二个值是第二个,依此类推是否有意义? Probably not. 可能不是。

My first question would be - do you actually use the user type anywhere in the database? 我的第一个问题是 - 你真的在数据库中的任何地方使用用户类型吗? If the answer is no, everything is easier, since you can simply use an enum and be done with it. 如果答案是否定的,那么一切都会变得更容易,因为你可以简单地使用枚举并完成它。

Otherwise, you probably should have a user type table as well, to get to use foreign keys properly. 否则,您可能还应该有一个用户类型表,以便正确使用外键。

Personally, I use manual ID for these - autogenerated keys can make a mess of your attempts to synchronize code and database. 就个人而言,我使用手动ID - 自动生成的密钥可能会使您尝试同步代码和数据库变得混乱。 Ideally, if your ORM allows it, you could have the code-database synchronization automatic - either through code generation, or through automatic database data update. 理想情况下,如果您的ORM允许,您可以通过代码生成或自动数据库数据更新自动进行代码 - 数据库同步。 If you can't, though, manually coding the enums (or some kind of pseudo enum) should still be a lot nicer in the code. 但是,如果不能,那么手动编码枚举(或某种伪枚举)应该在代码中更好。

As for localization, your options are completely the same. 至于本地化,您的选择完全相同。 Just use a resource key like "UserType-XXX", where XXX is the database ID of the type. 只需使用“UserType-XXX”之类的资源键,其中XXX是该类型的数据库ID。 If you want, you can also store the localized values in the database. 如果需要,还可以将本地化值存储在数据库中。 Just do whatever feels the best for your application. 只要做最适合您的应用的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM