2011-07-07 26 views
-1

我有一個客戶服務,允許消費者通過以下標準檢索客戶:CustomerId,DriversLicense,EmailAddress,PhoneNumber。我的問題是,如何構建針對此服務的請求/響應?我希望能夠請求多個這些標準並返回適當的響應。請記住,根據指定標準,未找到的客戶將返回null服務。服務請求/響應合同指南多個結果

幾個選擇跳到腦海,但我試圖找出什麼是真正的設計消耗品服務的正確方法:
1)對每個用例明確的合同:

// psuedo code for operations - proper request/response patterns apply normally 
Customer GetCustomerByCustomerId(string customerId); 
Customer[] GetCustomersByCustomerIds(string[] customerId); 
Customer GetCustomerByEmail(string emailAddress); 
// .. etc. etc. 

2)有一個單獨的請求來封裝所有的請求;但是,電話號碼不是唯一標識符。

// psuedo code for operations 
Customer[] GetCustomers(string customerId, string email, string phone, string dl); 

Customer[] GetCustomers(string[] customerIds, string[] emails, string[] phoneNumbers, string[] dls); 

我看到明確爲更具可讀性的選擇,但它很高興能夠一次提交所有這些信息到服務,並決定如何處理它。 #1的問題是它是多餘的,但我認爲它涵蓋了每個用例。 #2的問題是客戶可能會對哪個客戶屬於哪個請求感到困惑。謝謝您的幫助!

回答

0

一個快速解決方案是使用傳輸對象本身作爲搜索條件。這與query-by-example非常相似。

例如,重寫方法如

Customer[] getCustomers(Customer customerFilter) { 
    Query q; // = new query on customer table 
    if(customerFilter.isSetCustomerID()) { 
     // add where clause to the query: 
     //  Customer.CUSTOMER_ID = customerFilter.getCustomerID() 
    } 
    // for each field 
} 

Customer[] getCustomers(Customer[] customerFilters) { 
    Query q; // = new query on customer table 
    // add where clause to the query for each field: 
     // Customer.CUSTOMER_ID IN 
     //  [customerFilter : customerFilters].getCustomerID() 

}