2017-06-12 89 views
0

我正在實施一種方法,使用Vertx檢查數據庫中是否存在某個值,並使用HandlerAsyncResultVert.x處理程序檢查檢查存在的最佳做法是什麼?

我想知道哪一個是最好的做法:

選項1:如果沒有發現,處理程序與succeededFuture但結果爲FALSE:

public void checkExistence (..., String itemToFind, Handler<AsyncResult<Boolean>> resultHandler) { 
    // .... 
    doQuery(..., queryHandler -> { 
     if (queryHandler.succeeded()) { 
      List<JsonObject> results = queryHandler.result(); 
      boolean foundIt = false; 
      for (JsonObject json: results) { 
       if (json.getString("someKey").equals(itemToFind)) { 
        foundIt = true; 
        break; 
       } 
      } 
      resultHandler.handle(Future.succeededFuture(foundIt)); 
     } else { 
      resultHandler.handle(Future.failedFuture(queryHandler.cause().toString())); 
     } 
    }); 
} 

選項2:當沒有發現時,Handler與failedFuture:

public void checkExistence (..., String itemToFind, Handler<AsyncResult<Void>> resultHandler) { 
    // .... 
    doQuery(..., queryHandler -> { 
     if (queryHandler.succeeded()) { 
      List<JsonObject> results = queryHandler.result(); 
      boolean foundIt = false; 
      for (JsonObject json: results) { 
       if (json.getString("someKey").equals(itemToFind)) { 
        foundIt = true; 
        break; 
       } 
      } 
      // HERE IS THE DIFFERENCE!!! 
      if (foundIt) { 
       resultHandler.handle(Future.succeededFuture()); 
      } else { 
       resultHandler.handle(Future.failedFuture("Item " + itemToFind + " not found!")); 
      } 
     } else { 
      resultHandler.handle(Future.failedFuture(queryHandler.cause().toString())); 
     } 
    }); 
} 

UPDATE

比方說,我有另一個例子,而不是檢查存在,我想獲得所有的結果。我是否檢查空的結果?我是否將視爲失敗或成功?

選項1:只有將他們輸出時,它不是空或空,否則,達不到它

public void getItems(..., String itemType, Handler<AsyncResult<List<Item>>> resultHandler) { 
    // .... 
    doQuery(..., queryHandler -> { 
     if (queryHandler.succeeded()) { 
      List<Item> items = queryHandler.result(); 
      if (items != null && !items.empty()) { 
       resultHandler.handle(Future.succeededFuture(items)); 
      } else { 
       resultHandler.handle(Future.failedFuture("No items found!")); 
      } 

     } else { 
      resultHandler.handle(Future.failedFuture(queryHandler.cause().toString())); 
     } 
    }); 
} 

選項2:輸出結果我得到了,即使它可能是空的或null

public void getItems(..., String itemType, Handler<AsyncResult<List<Item>>> resultHandler) { 
    // .... 
    doQuery(..., queryHandler -> { 
     if (queryHandler.succeeded()) { 
      List<Item> items = queryHandler.result(); 
      resultHandler.handle(Future.succeededFuture(items)); 
     } else { 
      resultHandler.handle(Future.failedFuture(queryHandler.cause().toString())); 
     } 
    }); 
} 

回答

2

第1個選項更好,因爲您可以清楚地說,checkExistence返回了TrueFalse併成功完成或失敗,出現一些異常(數據庫問題等)。

但讓我們說,你決定堅持第二種選擇。然後,想象你有另一種方法:

void getEntity(int id, Handler<AsyncResult<Entity>> resultHandler); 

如果entity與提供id不存在,則拋出異常(使用Future.failedFuture)或退還null(使用Future.succeededFuture)?我認爲,你應該拋出異常,讓你的方法邏輯相似。但是,這又是一種特殊情況嗎?

對於返回實體列表的情況,您可以返回空列表,如果沒有實體。同樣適用於單個實體:最好返回Optional<Entity>而不是Entity,因爲這樣可以避免NullPointerException並且代碼中沒有可爲空的變量。有什麼更好的:Optional<List<Entity>>或空List<Entity>,這是開放的問題。

+0

請參閱我的問題更新。 – fluency03

+0

我已更新答案。 – berserkk

1

特別是如果你把它寫成可重用的代碼,那麼一定要選擇第一個選項。這種方法只是簡單地確定一個項目是否存在,所以應該簡單地返回它是否存在。該特定方法如何知道是否存在該項目不存在的錯誤條件?

某些調用者可能確定它確實是錯誤;那就是這種情況,那麼如果Future以false返回,它會拋出適當的異常。但另一個呼叫者可能只需在繼續之前知道該項目是否存在;在這種情況下,您會發現自己使用異常處理來組成業務邏輯。

+0

請參閱我的問題更新。 – fluency03

+1

關於更新,答案是一樣的(呃,嚴格地說,現在的答案是「隨着你的第二個選項」,因爲你改變了選項;))。同樣,這種方法的工作是返回儘可能多的結果。它沒有足夠的上下文來確定「0結果」是否是錯誤。這取決於誰打電話給它。事實上,我做了類似的事情(使用vert.x和RxJava),我正在尋找一個用戶。我返回一個 >>。可選要麼是空的,要麼包含找到的值。錯誤報告保留爲真正的錯誤(例如數據庫連接) –

相關問題