简体   繁体   English

什么时候应该使用枚举而不是域,反之亦然?

[英]When should I use enum instead of domain and vice versa?

My context: User will add one of numerous type of company into himself in the form. 我的上下文:用户将以表格的形式将众多类型的公司之一添加到他自己中。 For that form, I created enum to fill the listbox instead of pulling the database. 对于该表单,我创建了enum来填充列表框,而不是提取数据库。

I will try my best to explain what my question is: In my context, whats the difference between using enum and domain for showing and save into database? 我将尽力解释我的问题:在我的上下文中,使用enumdomain显示并保存到数据库之间有什么区别? Performance, logic, bestpractice? 性能,逻辑,最佳实践?

Because when user saves a companyType , it will save an int into database, but I can see what description it is. 因为当用户保存一个companyType ,它会将一个int保存到数据库中,但是我可以看到它的description using a method @Overide toString() . 使用方法@Overide toString()

Instead of using enum as code below, I could have created a domain with id and description moreover adding two registers into my db pulling into to form. 除了使用下面的enum作为代码外,我还可以创建一个具有iddescription的域,并且在我的数据库中添加两个寄存器以形成表单。

class Company{
    CompanyType companyType
}

Use this as enum 将此用作枚举

enum CompanyType {
    MARKET('Market', 1),
    SHOP('Shop', 2)
// ... etc of code
}

or use this as domain 或将此用作域

class CompanyType {
    Long id
    String description
}

sorry for my bad english. 对不起,我的英语不好。

您应该考虑的主要因素是公司类型数据可能发生的变化,如果您能够列出将要使用的所有可能的公司类型,并且确定不会频繁更新,那么枚举是合理的选择,没有另一方面,如果需要更改表或POJO,则要更改,添加,更新和删除公司类型,则表和POJO类是正确的选择。

two points: 两点:

  • performance: enum values are saved as a String (usually as enum.toString() ) in a db, and don't require any FK-relations and have less overhead during ORM transformations 性能:枚举值在数据库中保存为字符串(通常保存为enum.toString() ),并且不需要任何FK关系,并且在ORM转换期间开销较小

  • flexibility: enum values can be updated only with new deployment. 灵活性:枚举值只能使用新的部署进行更新。 If you need to change the value of the existing enum constant, you would have to migrate your data 如果需要更改现有枚举常量的值,则必须迁移数据

I would use enums instead of a domain class, as the performance is usually more important 我会用枚举代替域类,因为性能通常更重要

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

相关问题 我什么时候应该在 PriorityQueue 上使用 TreeMap,反之亦然? - When should I use a TreeMap over a PriorityQueue and vice versa? 何时使用TDD代替BDD,反之亦然 - When to use TDD instead BDD and vice versa 在 Java 中,我什么时候应该更喜欢 String 而不是 StringBuilder 反之亦然? - In Java, when should I prefer String over StringBuilder vice versa? 在可以使用地图的情况下使用枚举有什么好处,反之亦然? - Are there advantages to using an enum where you could use a map and vice versa? 将枚举转换为类,反之亦然 - Convert enum to class and vice versa 我应该使用哪个json库将我的对象数组(javascript)传递给服务器(java),反之亦然? - Which json library should I use to pass my array of objects (javascript) to the server (java) and vice versa? 我需要关于我的抽象 class 是否应该是接口的建议(反之亦然) - I need advice on if my abstract class should be an interface (or vice versa) 在java中何时使用向量而不是数组更好? - When is it better to use a vector than an array and vice versa in java? 何时在 LinkedList 或 ArrayList 上使用 HashMap,反之亦然 - When to use HashMap over LinkedList or ArrayList and vice-versa 无法将枚举类型转换为短类型,反之亦然 - Cant convert enum type to short and vice versa
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM