silverlight 上的 RIA 服务异步调用链接

发布于 2024-12-02 02:45:55 字数 635 浏览 0 评论 0原文

所以我正在尝试围绕 MVVM 和 RIA 服务进行思考。但我很快意识到他们不卖它。

我可以让事情发生,但一切看起来都很丑陋。例如,我有一个视图模型,需要从多个表等加载一组数据。

我当前的方法是链式异步操作。这味道真的很糟糕:

_Model.GetSomethingById(Id, result =>
            {
                _saveIt = result;
                _Model.GetSomethingElse(result2 =>
                    {
                        _saveit2 = result2;
                        //now i have all the data, can finally work on it.
                        //Initialize Grids, tables, input etc...
                    });
            });

这是使用 RIA 的预期方式吗?我缺少一些东西。

当然,我可以在服务端的查询中包含所有内容,也就是说,如果一切都相关的话。但这也很难闻。

So i'm trying to warp my head around MVVM and RIA services. But i'm quickly realizing its not like they sell it.

I can make things happen but everything seems ugly. For example i have a view model that needs a set of data to be loaded from multiple tables and etc.

My current aproach, chain async operations. This smells really bad:

_Model.GetSomethingById(Id, result =>
            {
                _saveIt = result;
                _Model.GetSomethingElse(result2 =>
                    {
                        _saveit2 = result2;
                        //now i have all the data, can finally work on it.
                        //Initialize Grids, tables, input etc...
                    });
            });

Is this the intended way to work with RIA? I'm missing something.

Sure i can include everything in the query at the service side, that is if everythings related. But that also smells bad.

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

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

发布评论

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

评论(4

意中人 2024-12-09 02:45:56

通过使用协程,您可以使代码更加简洁。将下面的代码与您在问题中包含的链接示例进行比较。

public IEnumerable<IAction> Activate()
{
    var action = build.Query(service => service.Authenticate(login, password));
    yield return action;

    if (!action.Result.Success)
        yield break;

    var user = action.Result.User;

    action = build.Query(service => service.GetIssues(user));
    yield return action;

    foreach (Issue each in action.Result)
    {
        Issues.Add(each);
    }
}

阅读以下内容: http://www.codeproject.com/KB/silverlight/FishingAllAsync .aspx

You can make your code much cleaner by using coroutines. Compare the code below to the chaining example you included in your question.

public IEnumerable<IAction> Activate()
{
    var action = build.Query(service => service.Authenticate(login, password));
    yield return action;

    if (!action.Result.Success)
        yield break;

    var user = action.Result.User;

    action = build.Query(service => service.GetIssues(user));
    yield return action;

    foreach (Issue each in action.Result)
    {
        Issues.Add(each);
    }
}

Have a read of this: http://www.codeproject.com/KB/silverlight/FixingAllAsync.aspx

橘虞初梦 2024-12-09 02:45:55

这基本上就是你的代码应该如何与 MVVM & 一起工作。 RIA 服务。不过,我还有一些提示:

  • 通常,在检索数据后,ViewModel 不会触发网格等的初始化。相反,它通常通过与 XAML 绑定来完成。即,如果您要绑定的 ViewModel 中有一个 Entity 对象,那么最好有一个默认实例,然后将实体的内容从结果复制到示例中的 _saveIt 。这将导致 XAML 控件中的绑定自动更新。

  • 在 ria 服务中,当您等待来自服务器的数据时,通常有一个 BusyIndi​​cator 控件绑定到 ViewModel 中的 bool 属性。这有助于在初始化后等待来自服务器的数据时显示 GUI。

  • 如果您正在做任何类型的数据表,我强烈推荐 RIA Services 1.0 SP1,它有一些内置的 DomainCollectionViewDomainCollectionViewLoader 类,用于处理分页、绑定我绕了一段时间试图根据一些早期的论坛帖子自己实现这个,直到我发现它包含在 SP1 中。

总的来说,你走在正确的轨道上。当您从 XAML 进行绑定时,RIA 服务中有很多异步代码可以很好地处理。

This is basically how your code should work with MVVM & RIA Services. I would have a few more tips, however:

  • Usually the initialization of Grids, etc., after you've retrieved data is not triggered from your ViewModel. Instead, its usually done through Binding with your XAML. Ie, if you have a single Entity object in your ViewModel that you're binding to, it would be preferable to have a default instance, and then copy the contents of the entity from result to _saveIt in your example. This would cause your binding in your XAML controls to just automatically be updated.

  • Its common in ria services to have a BusyIndicator control that's bound to a bool property in your ViewModel while you're waiting for data from the server. This can help with the appearance of the GUI while you're waiting for data from the server after initialization.

  • If you're doing any kind of DataTables, I would highly recommend RIA Services 1.0 SP1, this has some built in DomainCollectionView and DomainCollectionViewLoader classes for handling paging, binding, etc. I went around in circles for awhile trying to implement this on my own based on some early forum posts until I discovered it was included in SP1.

In general you're on the right track. There's just a lot of asynchronous code with RIA Services that is handled nicely when you do Binding from your XAML.

辞慾 2024-12-09 02:45:55

我真的很喜欢 Visual Studio 添加的异步支持。这些位很早,但语法要好得多。它看起来像这样。

await TaskEx.WhenAll(
    this._context.Load(this._context.GetRedEntitiesQuery()).AsTask(),
    this._context.Load(this._context.GetGreenEntitiesQuery()).AsTask(),
    this._context.Load(this._context.GetBlueEntitiesQuery()).AsTask()
    );
// Now do stuff

我有一个 在这里发布相关内容

第一个 CTP 存在一些已知的兼容性问题,我不知道他们是否已经更新了。不过,值得检查一下。

I really love the Async support Visual Studio is adding. The bits are early, but the syntax is much better. It looks something like this.

await TaskEx.WhenAll(
    this._context.Load(this._context.GetRedEntitiesQuery()).AsTask(),
    this._context.Load(this._context.GetGreenEntitiesQuery()).AsTask(),
    this._context.Load(this._context.GetBlueEntitiesQuery()).AsTask()
    );
// Now do stuff

I have a post about it here.

There are some known compatibility issues with the first CTP and I don't know whether they've updated it yet. It's worth checking into, though.

撩起发的微风 2024-12-09 02:45:55

这没有什么问题。如果您愿意,可以创建单独的事件处理程序以使代码更漂亮。还要确保检查每个响应的错误。

There's nothing wrong about it. If you prefer, you can create separate event handlers to make the code prettier. Also make sure to check for errors on each response.

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