Answer the question
In order to leave comments, you need to log in
What layer should contain Queries in DDD?
I have a simple DDD service, with Article Aggregate root. I use MediatR and CQRS for separation of commands and queries. In DDD domain should not have dependencies on application and infrastructure layers. I have a repository IArticleRepository for composing some data from articles database. I have a rest endpoint for getting articles by some kind of filters so that I createArticleQuery : IRequest<ArticleDto(or Article)>
And when should this query object be? I have a repository per aggregate, so in Domain layer I have IArticleRepository. And I need to specify the input parameter type. If I put query in Infrastructure or Application layer I get the dependency from domain pointing to infrastructure or application. If I put query in Domain it violates DDD, because the query has no relations to business. If I will not putting an object, and just fields as a parameter to the repository, there will be about 10-15 parameters - this is a code smell.
It needed because in Query handler also appear SearchEngine logic, so I decided to encapsulate SQL logic from search engine logic in infrastructure via the repository or something like that.
Answer the question
In order to leave comments, you need to log in
example with 2 types (Article and User):
* InfrastructureLowLevel (know nothing about higher levels)
- DatabaseImplementation
- IQuery[TResult]
.. can combine SQL for DB query, or invoke specific IRepository methods (override-able in IRepository)
- IRepository[ TQuery, TResult]
methods:
\ TResult GetSingleByQueiry(TQuery IQuery)
\ List[TResult] GetMultiByQueiry(TQuery IQuery)
.. invokes DatabaseImplementation
.. invokes IQuery for specific actions - conditions by type
* InfrastructureHighLevel (semi-domain, know about InfrastructureLowLevel):
- Article
- User
- ArticleQuery : IQuery[Article]
- UserQuery : IQuery[User]
- ArticleRepository : IRepository[ArticleQuery, Article]
- UserRepository : IRepository[UserQuery, User]
* BisinessDomain (higher\highest level, know about InfrastructureHighLevel):
- MyLogic
usage:
User user = UserRepository.GetSingleByQueiry(UserQuery)
Article article = ArticleRepository.GetSingleByQueiry (ArticleQuery)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question