开发者

When working with objects that are saved to a database that have a relationship, do you load the whole objects or just the id?

开发者 https://www.devze.com 2023-02-19 15:35 出处:网络
Suppose I have a CatHerder and he has Cats, when I getCatHerderByID(String id) should I load in the CatHerder and set an arrayList of cats or an arrayList of cat开发者_JS百科IDs? getCatHerderByID() sh

Suppose I have a CatHerder and he has Cats, when I getCatHerderByID(String id) should I load in the CatHerder and set an arrayList of cats or an arrayList of cat开发者_JS百科IDs?


getCatHerderByID() should be a method which returns an object of type CatHerder, which can have a property catsOwned which will contain a list of IDs, populated by the constructor. In addition, you can have a method on the CatHerder object called getCats() which returns an array of Cat consisting of the IDs in the CatHerder object. You shouldn't load unnecessary information just because it has a relationship with the information you actually want -- in many cases, everything will have a relationship with the data you actually want.


When you don't have much data or many concurrent users, you can go with either way.

One easy options would be to add an additional parameter specifying whether to return a "deep copy" including the relations, or just the attributes of the object itself.

Stay away from the urge to add more parameters, or you will end up re-implementing SQL :)

0

精彩评论

暂无评论...
验证码 换一张
取 消