我們在我們的CRM Silverlight開發中使用來自Microsoft的反應框架。我們只使用它的一小部分,但我們所做的是使所有將CRM稱爲IObservable的函數,然後訂閱它來監聽結果。最酷的是你可以將多個事件鏈接在一起並訂閱最終結果,因此它會一直等到所有事情完成。
下面是一個簡單的通話
public static IObservable<ProductGroup> RetrieveProductGroupByProductID(IOrganizationService service, Guid productID)
{
var res = RXCRMMethods.Retrieve(service, "product", productID, new ColumnSet() { Columns = new ObservableCollection<string> { "py3_productgroup" } });
return Observable.Create<ProductGroup>(observer =>
{
try
{
res.Subscribe(e =>
{
ProductGroup pg = new ProductGroup
{
ProductGroupId = e.GetAttributeValue<EntityReference>("py3_productgroup").Id
};
observer.OnNext(pg);
},
ex =>
{
observer.OnError(ex);
});
}
catch (Exception ex)
{
observer.OnError(ex);
}
return() => { };
});
}
的例子,在這裏是如何訂閱多個呼叫
var LoadQRY = from MatExResult in MaterialExclusionsFactory.GetMaterialExclusionsForOpportunity(this.Config.CrmService, this.OpportunityID)
from result in QuoteLineItemFactory.RetrieveQuoteLineItems(Config.CrmService, this)
from crateResult in QuotePackingCrateFactory.GetOneOffCratesForQuote(this.Config.CrmService, this.QuoteId)
from StaticResult in QuoteLineItemFactory.RetrieveStaticQuoteLineTypes(this.Config.CrmService,this)
select new { MatExResult,result,crateResult,StaticResult };
LoadQRY.Subscribe(LoadResult =>
{
//Do something
},ex=>
{
//Catch errors here
});
簡單的方法:調用新的異步內完成前面的異步調用的。 –
現在我已經採用了這種簡單的方法,但是應該做一些更硝和更乾淨的戰爭來做到這一點。我想找到它。 –