Repository class in DDD

855 Views Asked by At

I am trying to follow DDD and I have a Question class and a Feedback class (among others). I want to be able to count the number of questions, number of feedbacks and many other things which is considered meta operations.

Should such "meta" methods be in the same repository as the other methods belonging to the class, or should they be in a MetaRepository where you have different meta methods that queries the database (in this case all classes will be mixed)?

4

There are 4 best solutions below

0
Dmitry On BEST ANSWER

Nothing in DDD prohibits having more than one repository per aggregate. You can simply have one repository for basic queries and lifecycle methods (IQuestionsRepository) and a separate repository for what you call 'meta' or 'statistics' purposes (IQuestionsStatistics). This works very well for a larger domains where following one-repository-per-aggregate principle may result in 'method explosion' and SRP violation. Following DDD should not go against basic OOP principles.

0
Francois On

To me, each repository is responsible for counting its elements, it's one method among getById, getAll... (standard methods).

0
Domenic On

Are these questions ("how many feedbacks?") and the quantities they're referring to an important part of your domain?

If so, they seem like reasonable queries to put on your repository. Your repository's job, after all, is to answer domain-relevant questions about the aggregates it contains.

If not, e.g. if this is just incidental stuff perhaps for display to the user, then these questions might belong in an application service, outside of the domain layer entirely.

0
Anthony Shaw On

I would stick to more of a single repository per domain model class, or use a generic repository

I've personally never seen anybody create a MetaRepository for the purpose that you describe