2009-09-16 68 views
0

我需要使用WCF API將數據保存到數據庫中。通常情況下,我會使用鏈接,如下面的例子:線程模式:鏈接和循環

IClientBroker clientBroker = UIContext.CreateWcfInterface<IClientBroker>("Data/ClientBroker.svc"); 
    clientBroker.BeginSetClientBusinessName(_client.ID, businessName, (result) => 
     { 
      _client = ((IClientBroker)result.AsyncState).EndSetClientBusinessName(result); 

        clientBroker.BeginSetClientAddress(_client.ID, addressObservableCollection, postcodeZip, (result2) => 
       { 
        _client = ((IClientBroker)result2.AsyncState).EndSetClientAddress(result2); 

          clientBroker.BeginSetClientTelephone(_client.ID, telephone, (result3) => 
         { 
          _client = ((IClientBroker)result3.AsyncState).EndSetClientTelephone(result3); 

            clientBroker.BeginSetClientFax(_client.ID, fax, (result4) => 
           { 
              _client = ((IClientBroker)result4.AsyncState).EndSetClientFax(result4); 

              if (customFields.Save(validationSummaryBridge)) 
              { 
               CloseWindow(true, "ClientID=" + _client.ID.ToString()); 
              } 
              else 
              { 
               validationSummary.Errors.Add(new ValidationSummaryItem("Failed to save Custom Fields")); 
              } 
           }, clientBroker); 
         }, clientBroker); 
       }, clientBroker); 
     }, clientBroker); 
} 

這給了我假同步行爲,我需要這樣拋出異常及時和我可以確認的事件做出反應。

但是,這並沒有很好的映射,當我有一個字段的循環來保存。例如,哪種模式最適合保存以下「自定義字段」列表,其中每個自定義字段必須使用單個WCF調用進行保存?

ICustomFieldsBroker customFieldsBroker = UIContext.CreateWcfInterface<ICustomFieldsBroker>("Data/CustomFieldsBroker.svc"); 
    foreach (CustomField customField in _customFields) 
    { 
     string newValue=_customFieldControlDictionary[customField].CustomField.Value; 
       customFieldsBroker.BeginSetCustomFieldValueForItem(DataTypeID, DataItemID, customField.Key, newValue, (result) => 
      { 
         ((ICustomFieldsBroker)result.AsyncState).EndSetCustomFieldValueForItem(result); 
      }, customFieldsBroker); 
    } 

在上面的例子,這將只是掀起,比方說,5個請求給WCF API /線程這將潛在地返回後的形式已經關閉。我需要他們「排隊」,所以我可以列出他們的狀態並返回到表單。

非常感謝。

不要讓WCF分散注意力,但如果您有任何意見,請告訴我。 :)

+0

代碼格式化....有趣.... – 2009-09-16 10:58:30

+0

和你的生產點是......太多的嵌套?因此,建議在保持某種級別的代碼封裝的同時做一個更好的方法。 – 2009-09-16 11:41:29

回答