假设您有一个分层的项目,分为表示层,业务层和数据访问层.如果使用的是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的位置
发布评论