gpt4 book ai didi

c# - 在 CQRS 中,我的读取端应该返回 DTO 还是 ViewModel?

转载 作者:IT王子 更新时间:2023-10-29 04:09:25 26 4
gpt4 key购买 nike

我正在与我的同事就 CQRS 应用程序读取端的设计进行辩论。

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

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,例如:

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

从我的同事和我对此事所做的研究来看, react 似乎不一 - 看起来它真的取决于上下文。所以我问你,我亲爱的 StackOverflowians:

一种方法似乎比另一种方法有明显的优势吗?如果有,它们是什么?

最佳答案

一般建议是每个屏幕一个投影(Greg Young)或者甚至每个小部件一个投影(如果我对 Udi Dahan 的理解正确的话)。

将读取模型合并到必须再次映射到单独 View 的 DTO 与优化读取模型的全部目的相矛盾。它增加了我们最初试图摆脱的复杂性和映射步骤。

我的建议:如果在薄读取层中使用 NoSQL,请尝试尽可能接近 SELECT * FROM ViewSpecificTable [WHERE ...] 或类似的东西。

聚合根及其“子代”的概念在读取端没有太多含义,因为它们是领域模型概念。您不希望在读取端有域模型,它只存在于写入端。

Mohamed Abed 提到的 UI 平台特定转换应该在 UI 层中完成,而不是在读取模型本身中完成。

长话短说:我会选择选项 2。为了不将它与特定于平台的 ViewModel 混淆,我宁愿称它为 ReadModel 但有一个无论如何,每次观看。

关于c# - 在 CQRS 中,我的读取端应该返回 DTO 还是 ViewModel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7747727/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com