title: Designing a class diagram for a domain model
body: <p>First, don't think i'm trying to get the job done by someone else, but i'm trying to design a class diagram for a domain model and something I do is probably wrong because I'm stuck, so I just want to get hints about what i'm not doing correctly to continue...</p>
  
  <p>For example, the user needs to search products by categories from a product list. Each category may have subcategories which may have subcategories, etc.</p>
  
  <p>The first diagram I made was this (simplified):
  <a href="https://i.stack.imgur.com/TCCkB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TCCkB.png" alt="enter image description here"></a></p>
  
  <p>The user also needs to get a tree list of categories <strong>which have at least one product</strong>.
  For example, if this is <strong>all</strong> the categories tree:</p>
  
  <ul>
  <li>Music instruments
  
  <ul>
  <li>Wind</li>
  <li>String
  
  <ul>
  <li>Guitars</li>
  <li>Violins</li>
  </ul></li>
  <li>Percussion</li>
  </ul></li>
  <li>Books
  
  <ul>
  <li>Comics</li>
  <li>Fiction</li>
  <li>Romance</li>
  </ul></li>
  </ul>
  
  <p>I can't return a tree of Category <strong>which have at least one product</strong> because I would also get all subCategories, but not each sub category has a product associated to it.</p>
  
  <p>I also can't remove items from the Category.subCategories collection to keep only items which have associated products because it would alter the Category entity, which may be shared elsewhere, this is not what I want.</p>
  
  <p>I thought of doing a copy, but than I would get 2 different instances of the same entity in the same context, isn't it a bad thing ?</p>
  
  <p>So I redesigned to this:
  <a href="https://i.stack.imgur.com/sJire.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sJire.png" alt="enter image description here"></a></p>
  
  <p>Now I don't get a collection of child categories I don't want with each Category, I only know about its parent category, which is ok.</p>
  
  <p>However, this creates a tree of categories which is navigable only from the bottom to the top, it makes no sense for the client of ProductList who will always need a top -> bottom navigation of categories.</p>
  
  <p>As a solution I think of the diagram below, but i'm not sure it is very good because it kinda dupplicates things, also the CategoryTreeItem does not seems very meaningful in the domain language.
  <a href="https://i.stack.imgur.com/lf1YG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lf1YG.png" alt="enter image description here"></a></p>
  
  <p>What am I doing wrong ?</p>