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
});