简体   繁体   中英

Should I use other entity objects or their ids

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

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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