有链接的异步/事件调用有用的设计模式吗? [英] Is there a useful design pattern for chained asynchronous/event calls?

查看:194
本文介绍了有链接的异步/事件调用有用的设计模式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前不得不在Silverlight中集成许多Web服务调用,使得类似于下面的代码。在所有3的加载完成之前,不能进行任何用户交互。

I'm currently having to integrate a lot of web service calls inside Silverlight that make calls similar to the code below. No user interaction should be possible until the loading of all 3 is complete.

// In my view, having standard delegate methods (non-anonymous) makes the 
// code below even messier.

// I've left out the EventArgs to shorten the example however
// I usually use these objects in the delegate method.
MyService1 service1 = new MyService1();
service1.GetAllUsersCompleted += delegate
{
    MyService2 service2 = new MyService2();
    service2.AnotherTaskCompleted += delegate
    {
        MyService3 service3 = new MyService3();
        service3.YetAnotherTaskCompleted += delegate
        {
            // etc.
        };
        service3.YetAnotherTask();
    };
    service2.AnotherTask();
};
service1.GetAllUsers();

本质上,我希望调用是同步的,因为Silverlight中的Web服务代理类的架构(一切都是事件驱动的)我不能这样做。

Essentially I would like the calls to be synchronous however because of the architecture of web service proxy classes in Silverlight (everything is event driven) I can't do this.

我的问题是,是否有一个更优雅的解决方案或可能是一个以.NET为中心的设计模式,用于重构我远离一个委托方法调用另一个方法,调用另一个方法,更容易管理的东西?

My question is, is there a more elegant solution or possibly a .NET centric design pattern for refactoring what I have away from one delegate method calling another method, calling another, into something that is easier to manage?

我最大的问题之一是代码的实际方法调用来在代表的大部分工作之后。

One of my biggest issues with the code is the actual method call comes after the bulk of the work in the delegate.

推荐答案

嗯,我建议你先利用异步的性质Silverlight通过同时运行所有三个电话(如果可能的话)。

Well, I'd recommend first that you take advantage of the asynchronous nature of Silverlight by running all three calls simultaneously, if possible.

您可以按顺序执行任务(特别是任务延续),但您也可以看看 Rx library ,旨在优雅地处理这种情况。

You could do this "sequentially" with tasks (task continuations, in particular), but you may also want to look at the Rx library, which is designed to gracefully handle this kind of situation.

另一个最后提示:不推荐用户交互。至少给他们取消操作的选项。

Another final tip: disallowing user interaction isn't recommended. At least give them the option of cancelling the operation.

这篇关于有链接的异步/事件调用有用的设计模式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆