12

If you follow the repository pattern they... say to create a repository for each root aggregate entity.

That means when I have this model:

customer has orders order has products product has supplier

etc...

That would mean I have 4 repositories which are put into ONE repo. customer is the root entity.

Do I misunderstand here something?

Elisabeth
  • 20,496
  • 52
  • 200
  • 321

2 Answers2

16

It is correct that you should have a repository per aggregate. What can vary however is the set of aggregates in your domain. The Customer/Order/Product/Supplier model can be decomposed into aggregates in several ways. The decomposition into aggregates depends on a variety of factors and is contingent upon the domain at hand.

An aggregate should be a consistency boundary meaning that it defines what set of entities should be consistent in the context of behaviors associated with those entities. Given this constraint, object references between aggregates should be eliminated and replaced with identity references.

In your model, it could be that customer,order,product and supplier are distinct aggregates and would therefore require separate repositories. Even though customer is an aggregate root (part of the customer aggregate) and order depends on customer, it does not mean that the customer repository should contain the order repository. The order repository should be completely separate, since order is a the root of the order aggregate.

Take a look at Effective Aggregate Design by Vaughn Vernon for in-depth treatment of how to design aggregates.

eulerfx
  • 36,769
  • 7
  • 61
  • 83
  • Would you please correct this:"...since order is an aggregate root of the order aggregate" – Elisabeth Feb 07 '13 at 19:09
  • I changed the wording a bit, but I'm not sure what you wanted corrected? – eulerfx Feb 08 '13 at 00:44
  • hm I guess I do not understand why order is the root of the order aggregate. Can you tell me where order would not be the root of the order aggregate? Thanks for the link I have bookmarked it. – Elisabeth Feb 11 '13 at 18:43
  • You have an aggregate called Order which represents an order. Each aggregate has a root entity, which in the case of the order aggregate is the order entity. A line item can be an entity within the order aggregate, however it is not the root of the aggregate. – eulerfx Feb 11 '13 at 18:52
  • Ok and I thought the root entity of the order aggregate is the customer entity... seems I have to learn more ;-) – Elisabeth Feb 12 '13 at 18:26
1

You have 4 entities related as you've outlined above and the repository implements the transaction context for all of those related entities.

pwnyexpress
  • 1,016
  • 7
  • 14