Eric Evan's DDD book, pg. 152:
Provide Repositories only for AGGREGATE roots that actually need direct access.
1. Should Aggregate Roots that don't need direct access be retrieved and saved via repositories of those Aggregate Roots that do need direct access?
For example, if we have Customer
and Order
Aggregate roots and if for whatever reason we don't need direct access to Order
AR, then I assume only way orders can be obtained is by traversing Customer.Orders
property?
2.
When should ICustomerRepository
retrieve orders? When Customer
AR is retrieved ( via ICustomerRepository.GetCustomer
) or when we traverse Customer.GetOrders
property?
3.
Should ICustomerRepository
itself retrieve orders or should it delegate this responsibility to a IOrderRepository
? If the latter, then one option would be to inject IOrderRepository
into ICustomerRepository
. But since outside code shouldn't know that IOrderRepository
even exists ( if outside code was aware of its existence, then it may also use IOrderRepository
directly ), how then should ICustomerRepository
get a reference to IOrderREpository
?
UPDATE:
1
With regards to implementation, if done with an ORM like NHibernate, there is no need for an IOrderRepository.
a) Are you saying that when using ORM, we usually don't need to implement repositories, since ORMs implicitly provide them?
b) I do plan on learning one of ORM technologies ( probably EF ), but from little I did read on ORMs, it seems that if you want to completely decouple Domain or Application layers from Persistence layer, then these two layers shouldn't use ORM expressions, which also implies that ORM expressions and POCOs should exist only within Repository implementations?
c) If there is a scenario where for some reason AR root doesn't have a direct access ( and project doesn't use ORM ), what would your answer to 3. be?
thanks