执行CQRS时ViewModel的位置

编程入门 行业动态 更新时间:2024-10-08 06:23:12
本文介绍了执行CQRS时ViewModel的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设您有一个分层的项目,分为表示层,业务层和数据访问层.如果使用的是CQRS,则将直接从数据访问层到表示层进行查询,并绕过业务层.

在这种情况下,如果您在表示层中使用ViewModels,则您的数据访问层将需要引用表示层,以根据表示层中的ViewModels返回数据.那不是反模式吗?

此处存在类似的问题- MVC 3应用程序中的模型,ViewModels,DTO

但是,如果您正在执行CQRS,则不会像答案中所述在ViewModel和Domain对象之间进行映射,因为您绕过了Domain/Business层,那么应该将ViewModel放在哪里?

解决方案

据我了解的CQRS,您将从查询端(也称为DataAccessLayer)获得DTO(DataTransferObjects),并将其传递到UI(PresentationLayer)./p>

这些DTO如果可以提供View的所有必要数据,则可以直接用作View的ViewModel,或可以与ViewModel中的其他DTO聚合.我认为这取决于视图中显示的数据.

要回答您的问题:ViewModels是PresentationLayer的一部分.

Suppose you have a layered project divided into the Presentation Layer, the Business Layer and the Data Access Layer. If you were using CQRS, you would be doing queries directly from the Data Access Layer into the Presentation Layer and bypassing the Business Layer.

In that case, if you are using ViewModels in your presentation layer, then your Data Access Layer would need reference to the Presentation Layer to return data in terms of the ViewModels in the presentation layer. Wouldn't that be anti-pattern ?

A similar question exists here - Models, ViewModels, DTOs in MVC 3 application

But if you are doing CQRS you will not be mapping between your ViewModel and Domain object as mentioned in the answer, since you are bypassing your Domain/Business layer Then where should you place your ViewModels ?

解决方案

As far as i understand CQRS you will get DTOs (DataTransferObjects) from the query side (aka. DataAccessLayer) that gets passed to the UI (PresentationLayer).

Theses DTO can directly be used as ViewModels for Views, if they provide all necessary data for the View, or can be aggregated with other DTOs in an ViewModel. I think it depends on the data that is presented in the View.

To answer your question: ViewModels are part of the PresentationLayer.

更多推荐

执行CQRS时ViewModel的位置

本文发布于:2023-11-25 06:13:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628631.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:位置   CQRS   ViewModel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!