Search code examples
asp.net-mvcviewmodeldtocqrs3-tier

ViewModel location when doing CQRS


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 ?


Solution

  • 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.