-1

I have two tables, Product and ProductCategory in a database.

Product has individual items like tv, book, food. ProductCategory has respective categories: Electronics, Office, Grocery, etc.

In DDD, would my code select all (the whole aggregate root), and are Product and Product Category in the same aggregate root?

If they are in the same Repository, would GetAllProduct, and GetAllProductCategory be two different methods at the same repository?

  • Possible duplicate of https://stackoverflow.com/questions/12870650/should-lookup-values-be-modeled-as-aggregate-roots – guillaume31 Oct 15 '18 at 11:45

2 Answers2

1

Every Repository should contain only Aggregates of a single type.

You should design your Aggregates based on the Bounded contexts and business invariants, not by tables and databases.

One hint when designing the Aggregates is that they are the biggest transactional boundary. That is, everything that happens inside an Aggregate is strongly consistent. Also, this means that a transaction should not span multiple Aggregates. If you need this then it means you haven't correctly designed your Aggregates.

From what I've seen so far in the wild and more importantly, from what I now from your business (almost nothing!), I would say that Product and ProductCategory are separate Aggregates, so they should stay in different repositories. But in order to give you a clear answer I would need to know what are the business invariants.

Constantin Galbenu
  • 16,951
  • 3
  • 38
  • 54
  • I can send more information, currently Producttable and Product table, have foreign keys, thats why I thought they are one aggregate –  Oct 15 '18 at 14:48
0

There should be two independent repository to handle database interaction for Product and ProductCategory in order to follow single responsibility principle.

I assume that you have only Fetch method for ProductCategory today but you may have requirement for data manipulation in future. It will be wise to keep them in separate repository to have a extensible design.

Sham
  • 910
  • 8
  • 15
  • ok, for some reason I thought they were in the same aggregate root, hence all in thesame repository, thanks- –  Oct 15 '18 at 04:53
  • That looks like the repository-per-table pattern and not really what is intended when using a repository in DDD. – Naeem Sarfraz Oct 22 '18 at 06:49