I have an aggregate root named Account
and an entity named Contact
that can be accessed through a method on the root: Account.GetContactById(string id)
. Access to the aggregate root is through a repository, so data access logic to get Accounts from storage resides there.
Where should the data access logic for accessing the Contact
entity reside? Most examples I see would show the Account.GetContactById
method searching an in-memory collection. In my case, an Account
can reference thousands of Contacts
which I would not want to prefetch into memory. So, given that access to data storage will be required when the method is called, do I implement that access in:
- The
Account.GetContactById
method? That would spread direct access to storage outside of repositories and introduce some tight coupling. - The
AccountRepository
, so it can be called by theAccount
aggregate? That would seem to exposeContact
entities directly to any other user of the repository, which violates Evans' rules. - Another repository, such as
ContactRepository
? In that case I have a repository for an entity that is not an aggregate root. - Other?