2
votes

I'm looking for clarification on the following 2 points with regards to DDD.

1.Can a domain entity call a repository?

2.Can a domain service call a repository?

Thanks

Edit

Found a suite of documents that detailed the DDD layers in lots of detail and this is what it said;

Additionally, and although as a rule Repositories are only used from the Application layer, it is also possible to make exceptions, and make queries invoking Repositories from the Domain services as necessary. But this should be avoided as much as possible in order to achieve homogeneity in our developments.

2

2 Answers

2
votes

I'm not aware of such hard and fast rules. Anything can be done.

But here's how I do it. I don't know if it qualifies as DDD:

  1. Usually domain objects do not call repositories. They need not be aware whether they are persisted or not.
  2. Services orchestrate domain objects and repositories to fulfill use cases. They own units of work and manage transactions.
2
votes

I wouldn't design a domain entity to reference a repository. A "repository" is rarely a part of the domain and would introduce a technical concern into your domain model. I've found repositories to fit best at the application services layer.