In CQRS, should my read side return DTOs or ViewModels?
c#, cqrs
Solution
The general advice is one projection per screen (Greg Young) or even one projection per widget (if I understand Udi Dahan correctly).
To consolidate read models into DTOs that once again have to be mapped into separate views contradicts the whole purpose of an optimized read model. It adds complexity and mapping steps that we tried to get rid of in the first place.
My advice: Try to get as close as possible to `SELECT * FROM ViewSpecificTable [WHERE ...]` or something similar if using NoSQL in your thin read layer.
The concept of Aggregate Roots and their "children" doesn't have too much of an implication on the read side, since these are domain model concepts. You don't want to have a domain model on the read side, it only exists on the write side.
UI platform-specific transformation as mentioned by Mohamed Abed should be done in the UI layer, not in the read model itself.
Long story short: I'd opt for option 2. To not confuse it with a platform-specific `ViewModel`, I'd rather call it `ReadModel` but have one per view anyway.
Problem
I'm having a debate with my coworkers in the design of the read side of a CQRS application. Option 1: The application read side of my CQRS application returns DTOs, e.g: ``` public interface IOrderReadService { public OrderDto Load(int id); } public class SomeController { public ActionResult SomeAction(int id) { var dto = ObjectFactory.GetInstance<IOrderReadService>().Load(id); var viewModel = Mapper.Map<OrderDto, SomeViewModel>(); return View(viewModel); } } public class SomeOtherController { public ActionResult SomeOtherAction(int id) { var dto = ObjectFactory.GetInstance<IOrderReadService>().Load(id); var viewModel = Mapper.Map<OrderDto, SomeOtherViewModel>(); return View(viewModel); } } ``` Option 2: The application read side returns ViewModels, e.g.: ``` public interface IOrderReadService { public SomeViewModel LoadSomething(int id); public SomeOtherViewModel LoadSomethingElse(int id); } public class SomeController { public ActionResult SomeAction(int id) { return View(ObjectFactory.GetInstance<IOrderReadService>().LoadSomething(id)); } } public class SomeOtherController { public ActionResult SomeOtherAction(int id) { return View(ObjectFactory.GetInstance<IOrderReadService>().LoadSomethingElse(id)); } } ``` From the research my coworkers and I have done on the matter, responses appear mixed - it looks like it really depends on context. So I ask you, my dear StackOverflowians: Does one approach seem to have clear advantages over the other? If so, what are they?