Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What should repositories in DDD return

I researched about repositories in DDD and found too much different thing. Everyone says different things about repositories and that made me confused.

I want to know:

  1. What methods should repositories contain?
  2. What should repositories definitely (or closer that) return?

Thanks.

like image 523
Furkan Avatar asked Sep 05 '25 17:09

Furkan


2 Answers

For each aggregate root (AR) you should have a repository. As a minimum the repository would probably have a void Save(Aggregate aggregate) and a Aggregate Get(Guid id) method. The returned aggregate would always be fully constituted.

I sometimes add methods for specific use cases in order to update only certain bits of data. For instance, something like void Activate(Guid id) or some such. This is simply to avoid manipulating more data than is necessary.

Querying on a repository is usually problematic since you should typically avoid querying your domain. For such scenarios my recommendation is to use a query mechanism that is closer to the data and in a more raw format than a domain object or object graph. The query mechanism would more-then-likely return primitives, such as int Count(Query.Specification specification) or perhaps return a list of read model instances.

like image 156
Eben Roux Avatar answered Sep 07 '25 17:09

Eben Roux


You are right, a repository has different meanings in different contexts - and many authors have their own interpretation. The way I understand them is from multiple perspectives:

  • They abstract away underline storage type
  • They can introduce interface closer to the domain model
  • They represent a collection of objects and thus serve as aggregate in-memory storage(collection of related objects)
  • They represent a transaction boundary for related objects.
  • They can't contain duplicates - like sets.
  • It is valid for the repository to contain only one object, without complex relations internally

So to answer your questions, repositories should contain collection related methods like add, remove, addAll, findByCriteria - instead of save, update, delete. They can return whole aggregate or parts of aggregates or some internal aggregate relation - it is dependent on your domain model and the way you want to represent objects

like image 24
Miroslav Trninic Avatar answered Sep 07 '25 17:09

Miroslav Trninic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!