哪一层应包含DDD中的查询 [英] What layer should contain Queries in DDD

查看:128
本文介绍了哪一层应包含DDD中的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的DDD服务,具有Article Aggregate根目录。我使用MediatR和CQRS分离命令和查询。在DDD域中,不应依赖于应用程序和基础结构层。我有一个存储库IArticleRepository,用于从Articles数据库组成一些数据。我有一个休息点,可以通过某种过滤器获取文章,以便创建

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

什么时候该查询对象应该是?我每个聚合都有一个存储库,因此在域层中有IArticleRepository。而且我需要指定输入参数的类型。如果我将查询放在基础结构或应用程序层中,则会从域中获得指向基础结构或应用程序的依赖关系。如果我将查询放在Domain中,它将违反DDD,因为查询与业务无关。如果我不放置对象,仅将字段作为存储库的参数,则将有大约10-15个参数-这是代码的味道。

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.

因为在查询处理程序中还会出现SearchEngine逻辑,所以我决定通过存储库或类似的方式将SQL逻辑与基础结构中的搜索引擎逻辑封装在一起。

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.

推荐答案

我通常会去各种查询层。就像我有一个 ICustomerRepository 处理我的聚合一样,我也有一个 ICustomerQuery 直接与我的数据存储交互。

I usually go for a query layer of sorts. Just as I would have an ICustomerRepository that handles my aggregates I would have an ICustomerQuery that interacts directly with my data store.

存储库实际上仅应用于聚合,因此仅用于数据修改。唯一的检索应该是检索整个聚合,以便对该聚合进行某种形式的更改。

A repository really should be only for aggregates and, therefore, only for data modification. The only retrieval should be retrieving an entire aggregate in order to effect some form of change on that aggregate.

查询层(比 layer 更受关注>,实际上)是基础架构。我通常还会在 Query 命名空间中为任何 read模型命名空间,以区分我的域 Customer ,例如,以及我的 Query.Customer

The query layer (more concern than layer, really) is infrastructure. I usually also namespace any read model in a Query namespace in order to distinguish between my domain Customer, say, and my Query.Customer.

这篇关于哪一层应包含DDD中的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆