0

As I'm drawing my project's classes diagram, I'm wondering...

Let's take Customer's relationship with Order for example.

It seems pretty natural and logic to aggregate Orders to the User... So should I? Because while I don't see concrete pros doing this (maybe you'd find some?), I see cons:

  • On Customer's instantiation, we have to inject the Order instances while we don't necessary need them...
  • The Customer's instantiation is slower than in the case of a simple relation because we have to query his orders everytime, even when we don't need them...

So in this example is it good or bad to aggregate orders to customers?


EDIT

We can lazy load aggregates/composites which eliminate the cons I've have listed, alright. Still, it forces us to lazy load instead of inject the orders as dependency... This might be a cons by itself?

I could use directly the order repository to get orders from Customer's id when I need and store the orders in a simple variable anywhere else in my code, not in the Customer's property. So I guess there wouldn't be aggregation anymore...

So I still wonder if there is a more pertinent reason to implement aggregation than just "it's what we should do"?


Classes Diagram


Sidenote: I have seen other related questions but their answers are just theoric and generic. It doesn't help me in anyway to define in this kind of case which relation type should I use.

  • 5
    The question is hard to understand. Naturally, we must associate an order with some user/customer, so that can't be the question. The question (literally) asks if it makes sense to use aggregation & composition when it makes sense, and since that is of course, true, that can't be the question. It seems like perhaps you are asking if: caching an orders list/array within the customer object is ok? – Erik Eidt Sep 3 '17 at 19:41
  • 2
    Are you aware of lazy initialization? Its seems your concerns would be adressed simply by using lazy initialization. – Polygnome Sep 3 '17 at 20:02
  • Thanks @Polygnome. Looks like lazy loading addresses perfectly the cons I've listed. :) You can post an answer relating about that, I'll "accept" it. Still, if you can think about some pros now that we have eliminated cons, you could share them too so the thread would make more sense... – user282139 Sep 3 '17 at 20:50
  • I still don't fully understand your cons. Why do you think you can't inject the dependencies with lazy loading? – Polygnome Sep 3 '17 at 23:25
  • Forgive me @Polyglone, the dependencies = orders. I reformulated it in my edit in case that's the reason of your doubt. – user282139 Sep 3 '17 at 23:39

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.