我正在嘗試確定用於商業密鑰驗證Web服務的最佳設計模式。基本邏輯流程編碼如下。該程序將採用一個參數並使用一個字段來幫助確定搜索可找到該業務密鑰的多個系統的路徑。首先搜索System1,如果未找到,則搜索System2和System3。 System1搜索邏輯取決於傳遞給原始驗證方法的參數中的字段。要使用哪種設計模式來實現此業務邏輯?
我不太確定使用哪種設計模式。它看起來像命令,責任鏈,模板方法都可以在這裏使用。
下面我的實現,我看到以下問題:
每個SearchSystemX方法需要知道返回NULL如果找不到業務鍵,使「控制」的方法將繼續搜索其他系統。
每個SearchSystemX都必須知道如何填充業務對象,目前只是通過一個簡單的基本字符串來實現,但僅作爲示例。
請讓我知道您的想法。
public string Validate (string parms) {
string returnValue = null;
returnValue = SearchSystem1(parms);
if (returnValue == null) {
returnValue = SearchSystem2(parms);
if (returnValue != null) {
returnValue = SearchSystem3(parms);
}
else if (returnValue == null) {
if (parms == "Criteria1") {
returnValue = SearchSystem4(parms);
if (returnValue == null) {
throw new ApplicationException("ID Invalid");
}
}
else if (parms == "Criteria2") {
throw new ApplicationException("ID Invalid");
}
}
}
return returnValue;
private string SearchSystem1 (string parms) {
string returnValue = null;
if (parms == "Criteria1") {
returnValue = SearchSystem1UsingColumn1(parms);
}
else if (parms == "Criteria2") {
returnValue = SearchSystem1UsingColumn2(parms);
if (returnValue == null) {
returnValue = SearchSystem1UsingColumn4(parms);
}
}
if (returnValue != null) {
returnValue = FetchXYCoordinate(parms);
}
return returnValue;
}
謝謝!
由於需求沒有指定需要動態構建鏈,這是CoR的核心優勢,這看起來像是過度殺傷。另外,這個鏈將被定義在哪裏? – 2009-10-23 19:25:00
我喜歡這個實現,但是在SearchSystem1中有基於條件的條件流。此外,有時如果它在一個系統中找到,這意味着我們想要從另一個系統收集額外的數據。不知道如何在鏈中表達,因爲你可以返回null或結果。 – JustinDSN 2009-10-28 12:55:55
很難說,你爲什麼不發佈代碼,看看是什麼 – OscarRyz 2009-10-28 15:23:25