哪种方法更好的做法? 我有一个带有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).
更多推荐
发布评论