かずきのBlog@hatena

すきな言語は C# + XAML の組み合わせ。Azure Functions も好き。最近は Go 言語勉強中。日本マイクロソフトで働いていますが、ここに書いていることは個人的なメモなので会社の公式見解ではありません。

Rx + WCF RIA Services = 簡単?? その3(via 非同期プログラミングはつらいよ(2))

前回書いたプログラムですが、LINQ星人として有名なid:neueccさんからZipメソッド内で副作用のあることをするのはいくない!って言われたので書き直してみました。

private EduDomainContext ctx = new EduDomainContext();

public void Load()
{
    this.IsBusy = true;
    // 読み込み開始!
    var cust = this.ctx.LoadAsObservable(this.ctx.GetCustomersQuery(1000));
    var supp = this.ctx.LoadAsObservable(this.ctx.GetSuppliersQuery(2000));

    cust.Zip(supp, (customer, supplier) => new { customer, supplier })
        .SelectMany(data =>
            this.ctx.LoadAsObservable(this.ctx.GetOrdersQuery(2000))
                .Select(order => new { data.customer, data.supplier, order }))
        .Subscribe(
            data =>
            {
                this.Customers.Source = data.customer;
                this.Suppliers.Source = data.supplier;
                this.Orders.Source = data.order;
            },
            ex =>
            {
                this.Message = ex.Message;
                this.IsBusy = false;
                throw new Exception("だめぽ", ex);
            },
            () =>
            {
                this.IsBusy = false;
            });

}

インフルで解熱剤が効いてる間に寝るのでコードを張るだけで失礼します。