CQS 和 ASP.NET MVC 操作

发布于 2024-09-12 02:02:35 字数 485 浏览 9 评论 0原文

读过CQS原理的人都知道:

CQS 规定每种方法都应该 要么是执行一个命令 操作,或返回数据的查询 给调用者,但不是两者。

说到 ASP.NET MVC Actions,CQS 是否表明我们不应该有这样的 Action?

public PartialView InsertOrder(Order order)
{
       OrderService.InsertOrder(order);
       return PartialView("OrderDetails", order);
}

该方法正在改变系统的状态并返回当前状态。如果这里应用 CQS,我们应该有 2 个单独的 Action:一个用于插入新订单,一个用于获取系统的系统(如果第一个 Action 成功完成,则应从客户端调用)。然而,这使编程变得复杂。

我想知道你对此的看法。

莫什

Those who have read about CQS principle know that:

CQS states that every method should
either be a command that performs an
action, or a query that returns data
to the caller, but not both.

Speaking of ASP.NET MVC Actions, does CQS indicate that we shouldn't have an Action like this?

public PartialView InsertOrder(Order order)
{
       OrderService.InsertOrder(order);
       return PartialView("OrderDetails", order);
}

This method is changing the state of the system and returning the current state. If CQS is applied here, we should have 2 separate Actions: one for inserting a new order and one for getting the system of the system (which should be called from the client if the first Action was completed successfully). However, this complicates programming.

I'd like to know your opinions on this.

Mosh

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

星軌x 2024-09-19 02:02:35

网络上命令/查询分离的常见示例是 Post/Redirect/Get

在 ASP.NET MVC 中,这通常以最简单的方式实现,例如

[HttpPost]
public ActionResult UpdateOrder(Order order){
  UpdateOrder(order);
  return RedirectToAction("ViewOrder", new { order.OrderId });
}

[HttpGet]
public ActionResult ViewOrder(int orderId){
  return View(GetOrder(orderId));
}

对于 AJAX 和部分视图,这可能不是最好的策略,因为 Post/Redirect/Get 解决的问题是“并不真正相关,并且重定向可能很棘手。

A common example of Command/Query Separation on the web is Post/Redirect/Get.

In ASP.NET MVC, this is usually implemented in the simplest way as

[HttpPost]
public ActionResult UpdateOrder(Order order){
  UpdateOrder(order);
  return RedirectToAction("ViewOrder", new { order.OrderId });
}

[HttpGet]
public ActionResult ViewOrder(int orderId){
  return View(GetOrder(orderId));
}

For AJAX, and a partial view, this might not be the best strategy, since the problems that Post/Redirect/Get solves aren't really relevant, and the redirect can be tricky.

十年九夏 2024-09-19 02:02:35

CQS 只关心对同一对象的命令和查询。由于 OrderView 和 Order 不是同一个对象(我从您的实现中猜想),该原则不适用,因此您的代码既不违背该原则,也不赞成:)

CQS is only concerned with command and queries to the same object. Since a OrderView the Order are not the same object (I guess from your implementation) the principle does not apply, so your code is not counter the principle neither in favor :)

素食主义者 2024-09-19 02:02:35

我从未听说过 CQS,但如果您正在执行 ASP.NET MVC(MVC 模式),那么您编写的操作完全没问题(假设此 OrderService 存在对真实服务的抽象)。控制器操纵模型并决定渲染哪个视图并将该模型传递给视图。

I've never heard of CQS, but if you are doing ASP.NET MVC (MVC pattern) the action you wrote is perfectly fine (assuming this OrderService there is an abstraction to the real service). The controller manipulates the model and decides which view to render and passes this model to the view.

小…楫夜泊 2024-09-19 02:02:35

我对埃菲尔铁塔时代的这个术语有一个模糊的记忆(如果一直追溯下去,实际上比当前的 oop 原则早了好十年左右(我认为是 80 年代末)。我建议这个术语和/或原则现在很可能已经过时,并被 mvc 中的 actionresults 所取代(无论是 asp 还是 codeignitor 等),我实际上认为就定义而言(我现在刚刚查找),这种分离与执行的逻辑有关。在您的示例中,即 OrderService.InsertOrder(order) 操作因此,在某种程度上,您的操作中执行的 mvc 实际上很不遵循此模式(InsertOrder 不会尝试提供任何状态信息,纯粹处理订单对象)

我建议您查看 ASP.NET MVC 的最佳实践,它基本上基于返回操作结果(或部分、内容结果等),此模式旨在简化范例以促进实现。以统一且普遍接受的方式提高生产力。

当然,您可以使用操作返回值来生成插入/更新/删除场景的成功或失败,然后根据这些返回值请求部分视图。然而,我个人认为我不会从该方法中利用太多价值,因为考虑到 MVC 中的控制器关心的是控制哪个视图应作为操作结果返回的逻辑。

希望这对

吉姆有帮助

I had a vague recollection of this term from the eiffel days (which if followed all the way back, actually predates most current oop principles by a good decade or so (late 80's i think). I'd suggest that this term and/or principle may well be outmoded now and superceded by actionresults in mvc (be that asp or codeignitor etc, etc). I actually think that in terms of the definition (which i just looked up now), this separation is concerned with the logic that performs the action i.e. OrderService.InsertOrder(order) in your example. So, in a way, mvc as performed in your action is actually loosley following this pattern (InsertOrder doesn't attempt to present any stateful info, purely process the order object).

I'd suggest that you look at best practice for asp.net mvc which is fundementally based on returning an actionresult (or partial, contentresult etc, etc). this pattern was designed to simplify the paradigm to facilitate productivity in a uniform and universally accepted fashion.

of course, you could use your action return values to generate a success or fail for the insert/update/delete scenarios and then request partial views based on those return value. However, i personally don't think i'd leverage too much value from that approach bearing in mind the fact that the controller in MVC is concerned with stearing the logic of which view should be returned as the result of an action.

hope this helps

jim

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文