使用ViewModels代替DTO作为CQRS查询的结果 [英] Using ViewModels instead DTOs as the result of a CQRS query

查看:178
本文介绍了使用ViewModels代替DTO作为CQRS查询的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读一个SO问题,我意识到我的Read服务可以提供一些 smarter 对象例如ViewModels而不是普通的DTO。这使我重新考虑读取服务返回的对象应该提供哪些信息

Reading a SO question, I realized that my Read services could provide some smarter object like ViewModels instead plain DTOs. This makes me reconsider what information should be provided by the objects returned by the Read Services

在使用DTO之前,读取服务只是将数据库查询的平面视图映射为类似哈希的结构

Before, using just DTOs, my Read Service just made flat view mapping of a database query into hash like structure with minimum normalization and no behavior.

但是我倾向于将ViewModel看作是更智能的东西。可能会生成数据库未提供的信息,例如状态图标,计算值,重新格式化的值,默认值等。

However I tend to think of a ViewModel as something "smarter" that can have generated information not provided by the database, like status icon, calculated values, reformatted values, default values, etc.

我开始看到某些ViewModel对象的构造可能如果我使通用的 ReadServiceInterface 仅返回ViewModel,则会变得更加复杂并具有潜在的缺点:

I am starting to see that the construction of some ViewModel objects might get more complicated and has potential downsides if I made my generic ReadServiceInterface return ViewModels only:


(1)我是否应该计划一些设计我的CQRS返回的ViewModel的限制?就像确保它们的构造几乎与普通DTO一样快?

(1) Should I plan some design restriction for the ViewModels returned by my CQRS? Like making sure that their construction is almost as fast as a plain DTO?

(2)本质上,DTO易于序列化并准备发送到SOA架构中的外部系统或嵌入式系统。变成一条消息。这是否意味着使用ViewModels将对我的体系结构产生负面影响?

(2) DTOs by nature are easily serialized and ready to be sent to an external system in a SOA architecture or embedded into a message. Does this mean that using ViewModels will have a negative impact on my architecture?

(3)我应该在读服务之外保留哪种ViewModel?

(3) Which type of ViewModels should I keep outside my Read Services?

(4)我是否应该从读取服务中检索所有ViewModel?

(4) Should I expect all ViewModels to be retrieved from Read Services?

过去,我实现了一些需要多个查询的ViewModel。我想在CQRS中,这是一种设计上的味道,因为它们提供的所有内容都应该仅在一个查询中。

In the past I implemented some ViewModels that needed more than one query. In a CQRS I suppose, that is a design smell, since everything they provide, should be in only one query.

我正在开始一个新项目,我以为在那里所有查询都将返回聚合对象或DTO。从那时起,ViewModels开始发挥作用。我想知道:

I am starting a new project, where I thought that any query will return either aggregate objects or DTOs. Since now ViewModels come into play. I am wondering:


(5)我应该计划架构中的查询产生两种类型的对象(ViewModels + Aggregates)还是三种(+ DTO)对象?

(5) Should I plan that queries within my architecture will yield two type of objects (ViewModels+Aggregates) or three (+DTO)?


推荐答案

视图模型(VM)提供一个主视图:视图。我们通常将VM视为一个相当愚蠢的对象,因此在这方面,VM和DTO之间没有技术上的区别,只是它们的用途和语义不同。

View Models (VM) serve a single master: the View. We're usually consider the VM a pretty dumb object so in this regard, there's no technical difference between a VM and a DTO, only their purpose and semantics are different.

如何构建VM是实现细节。某些VM是预先生成的,并存储在VM存储库中。其他服务由服务(或查询处理程序)实时构建,方法是直接查询数据库或查询其他存储库/服务,然后组合结果。没有对与错,也没有关于如何做的规则。

How you build a VM is an implementation detail. Some VM are pre generated and stored in a VM repository. Others are built in real-time by a service (or a query handler) either by querying the db directly or querying other repos/services then assembling the results. There's no right or wrong and no rules about how to do it. It comes down to preference.

在CQRS中,重要的部分是命令与查询的分离,即多个模型。对于应执行的查询次数或返回的视图模型或dto,没有任何规定。只要您至少有一个专门用于查询的读取模型,它就是CQRS。

In CQRS the important part is separation of commands from queries i.e more than one model. There's no rule about how many queries you should do or if you should return a view model or dto. As long as you have at least one read model dedicated for queries, it's CQRS.

不要让技术复杂化您的设计。正确的设计更多是关于高层结构而不是底层实现。之所以使用CQRS,是因为拥有读取模型可以简化您的应用程序,而不是出于其他原因。力求简化和简洁的代码,而不是硬性规定配方的严格规则。

Don't let technicalities complicate your design. Proper design is more about high level structure and not low level implementation. Use CQRS because having a read model simplifies your app, not for other reasons. Aim for simplification and clean code, not for rigid rules that dictate a 'how to' recipe.

这篇关于使用ViewModels代替DTO作为CQRS查询的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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