开发者

How to load data with class composition?

Here are my classes :

class PriceScale
-int ID
-float bookingFees
-Product product
-List<PricePeriod> periods

Class Product
-int ID
-string name
-List<PriceScale> priceScales

class PricePeriod
-DateTime begin
-DateTime end
-float price
-PriceScale scale

As you can see I strictly applied business rules "A product as many price scales, and a price scale has many period".

My problem : for instance when I'm dealing with the Product class I don't like to ask myself "Is the priceScales loaded ?" (cause I won't load it every time I need a product)

Solution :

1/ Lazy loading : I don't like it, cause you run sql query without even knowing it, and you can end up with a 1+n query problem (I'm working 开发者_JAVA技巧on the price computing system so I really need to be sure of which sql query is executed)

2/ Always load it : I don't like it, cause if I apply this logic to everything I'll end up loading the whole database.

3/ Remove the composition (ie the member priceScales),in that case what is the best way to deal with price scale :

  • get a Dictionnary<int,List<PriceScales>> where the key is the product id ?

  • something else ?

4/ Add at the beginning of my method where I need the priceScales

checkPriceScalesAreLoaded(productList);

It looks like lazy loading, but it's more explicit.

5/ Something else I didn't even think about :)

Thanks


To me this sounds like a persistence problem after all, not quite related to OOP or composition.

Are you loading your objects from a DB? If so, what language and persistence mechanism you are using? This influences possible solutions a lot.

For example with Java and Hibernate, the recommended way is to use the default lazy fetching strategy, then measure performance and fine-tune (selectively switching fetching strategy locally) as appropriate. You can switch to eager fetching, or batch/join queries, on individual mappings/properties/queries, while keeping the lazy fetching strategy on the rest.


I decided to make multiple result set queries like here

http://vb.net-informations.com/ado.net-dataproviders/ado.net-multiple-result-sets.htm

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜