LuckyLuke
LuckyLuke

Reputation: 49077

Repository class in DDD

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)?

Upvotes: 7

Views: 880

Answers (4)

Dmitry
Dmitry

Reputation: 17350

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.

Upvotes: 9

Anthony Shaw
Anthony Shaw

Reputation: 8166

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

Upvotes: 0

Domenic
Domenic

Reputation: 112827

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.

Upvotes: 0

Francois
Francois

Reputation: 10968

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

Upvotes: 1

Related Questions