我应该使用其他实体对象或它们的ID(Should I use other entity objects or their ids)

编程入门 行业动态 更新时间:2024-10-28 10:22:42
我应该使用其他实体对象或它们的ID(Should I use other entity objects or their ids)

哪种方法更好的做法? 我有一个带有Products表和Categories表的数据库。 产品总是有自己的类别。 现在我正在实现Product实体类,我不知道是否有更好的int属性“category_id”或具有Category类的对象属性。 所以如果构造函数应该是:

public Product(int id, int category_id)

要么

public Product(int id, Category category)

哪个更好? 或者我应该使用这两个属性?

感谢您的回复

Which approach is more best practice? I have a database with Products table and Categories table. Product always has its category. Now I am implementing the Product entity class and I do not know if it is better to have an int property "category_id" or object property with Category class. So if the contructor should be:

public Product(int id, int category_id)

or

public Product(int id, Category category)

Which of these is better? Or should I use both properties?

thanks for reply

最满意答案

第二个。 它更简洁,您可以随时更改类别标识符。

此外,它将更安全,因为您将无法将任意“int”值传递给第二个参数。 您不需要考虑哪个“int”负责“id”以及哪个“category_id”(如果缺少文档)。

The second one. It is more concise and you can always change the category identifier.

Additionally it will by more type safe, since you wouldn't be able to pass some arbitrary "int" value into the second parameter. You won't need to consider which "int" is responsible for "id" and which for "category_id" (in case of lack of documentation).

更多推荐

本文发布于:2023-07-30 02:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1321652.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实体   对象   ID   ids   objects

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!