对于链式异步/事件调用是否有有用的设计模式?

发布于 2024-09-10 03:37:17 字数 960 浏览 4 评论 0原文

我目前必须在 Silverlight 中集成大量 Web 服务调用,这些调用类似于下面的代码。在所有 3 个加载完成之前,不应进行任何用户交互。

// 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 服务代理类的体系结构(一切都是事件驱动的),我无法做到这一点。

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

我对代码的最大问题之一是实际的方法调用是在委托中的大部分工作之后进行的。

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

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倾城泪 2024-09-17 03:37:17

好吧,我首先建议您通过同时运行所有三个调用(如果可能)来利用 Silverlight 的异步特性。

您可以对任务(特别是任务延续)“按顺序”执行此操作,但您可能还想查看 Rx 库,旨在优雅地处理这种情况。

另一个最后的提示:不建议禁止用户交互。至少给他们取消操作的选择。

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

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.

[旋木] 2024-09-17 03:37:17

我认为这就是新任务(在并行库中)类尝试抽象的内容,但我可能是错的:)

I think this is what the new Tasks (in the Parallel library) classes try to abstract, but I could be wrong :)

笑梦风尘 2024-09-17 03:37:17

我在这里可能是错的,但代表们不是已经同步了。您可以使用 BeginInvoke 方法使它们异步。这是同步和异步方法调用的一个很好的资源,并解释了一些模式等。 http://support.microsoft .com/kb/315582

文章的标题可能是如何异步调用 Visual C# 方法,但大约一半时有同步方法调用的示例。

I might be wrong here but aren't delegates already syncrhonous. You can make them asynchronous by using the BeginInvoke method. Here is a good resource for syncronous and asynchronous method calls and explains some patterns etc. http://support.microsoft.com/kb/315582

The title of the article may be How to call a Visual C# method asynchronously but there are example of synchronous method calls about half way through.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文