English 中文(简体)
隔离
原标题:Command Query Separation violation

你认为什么

 if(!DoSomething()) return;

In Clean Code this is viewed as violation of Command Query Separation. But how can we understand if something in command DoSomething() went wrong? What about sql command (ex: void Delete(Table))? How can we know if that table existed?

感谢。

最佳回答

我同意Overwahn的评论,即你在发出<条码>的指令之前应检查<条码>。 在纯粹的CLQRS环境中,DoSomething不会归还任何东西,如果任何东西都无法阻止,也不会归还。 某些情况下(不是由于例外情况,而是种族状况或另一个在<条码>至和<条码>之间变化的事物),贵领域将发布<条码>DoSomethingWasInvalid(或像这样的活动),使您的申请最终得以一致。

It may sound complex, but it really becomes pretty simple once you start breaking down the logic into small chunks and allow your application to embrace eventual consistency.

多角度小组的DDD/CQRS小组拥有大量良好资源。 http://groups.google.com/group/ddcqrs/browse_thread/thread/6eb8e83acd59d8d/5a0ea089e8a43f65?lnk=gst&q=handle%20error%20in%20domain#5a0ea089e8a43f65”rel=“nofollow” 你如何告诉信使,指挥失败? 与你的问题相似。 象Udi Dahan、Greg Young、Rinat Abdullin等人监测这一群体,并提供一些确实巨大的答案。 我建议从现在起,也从现在起检查。

希望这一帮助!

问题回答

If something went wrong, DoSomething() should probably throw an exception, if you as the caller must handle it.
E.g.:

try  
{  
  DoSomething();  
  // .. do more after success  
}  
catch(SomeException ex) // maybe focus on a special error
{
  // maybe do something special or just clean up!  
}  

我将在埃夫勒撰写这一例子,使之简单易懂。

my_code
      -- Calls the `do_something  routine
   do
      set_table ("my_table")
      do_something
   end

do_something
      -- Something to do
   require
      valid_table: is_valid_table (table_name)
   do
      sql_list := execute_sql_on_table (table_name)
   ensure
      has_result: sql_list.count > 0
   end

sql_list: ARRAYED_LIST [STUFF]

table_name: STRING

set_table (a_name: STRING)
       -- Set `table_name  to `a_name 
   require
      has_name: not a_name.is_empty
      valid_table: is_valid_table (a_name)
   do
      table_name := a_name
   ensure
      table_name_set: table_name.same_string (a_name)
   end

delete_table (a_name: STRING)
      -- Delete `a_name  from the database.
   require
       valid_table: is_valid_table (a_name)
   do
      execute_sql ("DROP TABLE " + a_name)
   ensure
       table_gone: not is_valid_table (a_name)
   end
  1. The feature `do_something is a command, where the array sql_list is to be loaded with STUFF from the table "my_table".
  2. The precondition contract on do_something makes it the responsibility of the clientmy_code to provide table_name before making the call todo_something .
  3. In return, the post-condition ensure contract makes it the responsibility of the supplier do_something fill the arraysql_list with instances of STUFF.
  4. The feature `sql_list is a query, returning a reference pointer to an array of STUFF.
  5. Similarly, the feature table_name is a query returning a reference pointer to a STRING, which is set with a "setter" command calledset_table .

在这种情况下,“合同”是指确保适当区分关切,由谁负责上述小型法典。 《刑法》中明显缺乏TRY-CATCH的建筑。 在这种情况下,数据来源预计将具有“米-表”。 合同的存在意味着,如果合同失败,软件将产生例外。 要求失败的称呼器被打破,而供应商的定购后点失败。

最后,这部法典显示了明确的开端和合同规定的质量保证的应用。 因此,可以回答原来的问题:

“我们如何理解指挥吗? ql指挥(ex:真空删除(可下载))是什么? 我们如何知道这一表格是否存在?”

While it might be true that a call to delete_table ("my_table") might be injected into some ancestor or might happen on another thread, this is what the contracts are for in do_something . As long as those contracts stand guard over calls todo_something , the process will be appropriately handled. An injected call to `delete_table will simply cause the contract to fail.

所有这一切都假定,只有NOT OK ,DROP TABLE “my_table”,而这样做是不幸的。 然而,如果它变成了DROP TABLE“my_table”的基数,那么需要一个再工业机制或其他“handler”来管理这一使用案例,而上述准则将不起作用。

确实,现在我会想到这件事,[我们不认为“分离的因素”,而不是“关切的分离”!] i 了解这个专题,但太大地推动制定标准,将会导致诺爱。 从人类历史上看,很容易看到多少实践/标准被证明是错误的。 它 s尽了你的杰出观点。

在此情况下,总是想到与保留相反的情况。 i 只想说更多话,但我认为,从我的答案的角度来看,这是不够的;

good!





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签