返回一个任务违反CQS原则? [英] Is returning a Task violating the CQS-principle?

查看:127
本文介绍了返回一个任务违反CQS原则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • The CQS-principle (https://en.wikipedia.org/wiki/Command%E2%80%93query_separation) states that a command should return void.
  • The recommendation for async methods is to never return void (https://msdn.microsoft.com/en-us/magazine/jj991977.aspx), but instead returning a Task.

所以,如果我写一个异步的命令,将不可避免地打破CQS原则?

So, if I write an async command, will that inevitably break the CQS-principle?

推荐答案

在考虑你想知道(查询),或做(命令),那么,什么水平任务< T> 给你的数据,因此是一个正确的查询和任务没有,因此对于一个命令是正确的。 (返回void是汉语语言的一些前到preSS不返回数据的特定语言的方式)。

At the level of considering what you want to know (a query) or do (a command) then Task<T> gives you data and hence is correct for a query and Task does not and hence is correct for a command. ("return void" is the language-specific way for some langauges to express "do not return data").

在低于水平,其中你正在考虑由异步操作管理的机制,那么你总是希望有关于异步操作的状态信息,因此总是需要某种任务的对象。那是不是要考虑命令查询分离的水平。

At the level below that, in which you are considering the mechanism by which asynchronous operations are managed, then you always want to have information about the state of the asynchronous operation and so always want some sort of task object. That is not the level to consider command-query separation.

可比,如果一个.NET方法调用到COM方法,它会被调用到code,它总是返回指示成功或失败的值。这仅仅是一个的异常处理如何发生在那个特定的技术实现细节。这是不是至关重要的或不相关的思考这个问题取决于你在工作的水平。 工作是如何基于任务的异步编程工作的实现细节。这是不是至关重要的或不相关的思考这个问题取决于你在工作的水平。

Comparably, if a .NET method called into a COM method it would be calling into code that always returned a value indicating success or failure. That is just an implementation detail of how exception-handling happens in that particular technology. It's either vital or irrelevant to think about this depending on the level you are working at. Task is an implementation detail of how task-based asynchronous programming works. It's either vital or irrelevant to think about this depending on the level you are working at.

在您需要思考的水平,这是一个命令或查询工作是关于你如何让无效。

The level at which you need to think about "is this a command or a query", Task is an implementation-detail about how you get void.

这篇关于返回一个任务违反CQS原则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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