Is returning a Task violating the CQS-principle?

前端 未结 3 688
花落未央
花落未央 2021-01-18 07:13
  • The CQS-principle (https://en.wikipedia.org/wiki/Command%E2%80%93query_separation) states that a command should return void.
  • The recommendation for async met
3条回答
  •  深忆病人
    2021-01-18 08:16

    At the level of considering what you want to know (a query) or do (a command) then Task 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.

    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.

提交回复
热议问题