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 :)
精彩评论