在CQRS中,我的阅读方是否应返回DTO或ViewModels? [英] In CQRS, should my read side return DTOs or ViewModels?

查看:79
本文介绍了在CQRS中,我的阅读方是否应返回DTO或ViewModels?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与同事讨论CQRS应用程序的读取方面的设计。

I'm having a debate with my coworkers in the design of the read side of a CQRS application.

选项1: CQRS应用程序的应用程序读取端返回DTO,例如:

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);
    }
}

选项2:应用程序读取端返回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));
    }
}

根据我和我的同事所做的研究,事情,答案似乎是混杂的-看起来它确实取决于上下文。所以我问你,亲爱的StackOverflowians:

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?

推荐答案

一般建议是每个屏幕投影一次(格雷格·杨),甚至每个小部件一个投影(如果我正确理解了Udi Dahan)。

The general advice is one projection per screen (Greg Young) or even one projection per widget (if I understand Udi Dahan correctly).

要将读取模型整合到DTO中,这些DTO必须再次映射到单独的视图中,这与优化读取模型的整个目的背道而驰。

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.

我的建议:尝试尽可能地接近 SELECT * FROM ViewSpecificTable [WHERE ...] 或类似的内容(如果在瘦读取层中使用NoSQL的话)。

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.

Mohamed Abed提到的特定于UI平台的转换应该是

UI platform-specific transformation as mentioned by Mohamed Abed should be done in the UI layer, not in the read model itself.

长话短说:我选择选项2。不要将其与特定于平台的 ViewModel ,我宁愿将其命名为 ReadModel ,但无论如何每个视图都有一个。

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.

这篇关于在CQRS中,我的阅读方是否应返回DTO或ViewModels?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆