In CQRS, should my read side return DTOs or ViewModels?

前端 未结 4 1135
太阳男子
太阳男子 2020-12-23 16:59

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 applicat

相关标签:
4条回答
  • 2020-12-23 17:39

    One of the primary principles of DDD/CQRS is that you shouldn't be editing the view model. Instead, task-based screens should guide the user towards issuing explicit commands. If you can't come up with task-based screens, you should be using a different form of CQRS like the one I describe here:

    http://udidahan.com/2011/10/02/why-you-should-be-using-cqrs-almost-everywhere/

    0 讨论(0)
  • 2020-12-23 17:48

    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.

    0 讨论(0)
  • 2020-12-23 17:49

    A read model is a projection of the write model. It's there to fulfill a specific purpose. In your case that seems providing view models to your MVC controllers. As such, why go thru the trouble of mapping DTO's to Viewmodels? The only reason I could think of was that the benefit of having two separate read models might not outweigh their maintenance cost. But do consider that "merging" read models for the purpose of "reuse" and "lowering maintenance costs" increases complexity for the developer (Can I change this table? Hmmm, I now have two (or more) consumers I have to take into account - smells a bit like database integration all over again).

    Just my thoughts.

    0 讨论(0)
  • 2020-12-23 17:55

    I would prefer to return DTO to separate the application layer from presentation technology (because each presentation technology may have some requirements on the structure of presentation model) for example a web MVC application binding is different than WPF MVVM binding, also you may require some properties/fields in view models that has nothing to do with application data like for example (SliderWidth, or IsEmailFieldEnabled, ...). Also for example if using WPF MVVM i would need to implement INotifyPropertyChanged interface to allow binding it is not convenient nor related to implement this interface in the application read service.

    so i would prefer separating the concern of read data representation from the actual presentation technology and view model.

    So option 1 is better for me

    0 讨论(0)
提交回复
热议问题