@andreyka26

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 create

ArticleQuery : IRequest<ArticleDto(or Article)>
And when this query object should 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.
  • Вопрос задан
  • 476 просмотров
Пригласить эксперта
Ответы на вопрос 1
@AlexHell
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)
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
21 июн. 2024, в 22:12
5000 руб./за проект
21 июн. 2024, в 21:41
20000 руб./за проект
21 июн. 2024, в 21:34
20000 руб./за проект