使用异步调用进行分层数据检索和显示的模式?

发布于 2024-11-08 20:34:11 字数 1906 浏览 0 评论 0原文

上下文 Silverlight/WPF、C#、.NET 4,

我有一个 4 级深的缩略图树,我需要以某种有意义的方式枚举和显示。 对于同步执行(顺序世界),我们可以这样思考

    Channels = Channels_Build("CHANNELS.XML");
    foreach Ch in Channels
    {
         Cats = Cats_Build( Ch.URL ) ;
         foreach Cat in Cats
         {
             PLs = PLs_Build( Cat.URL ) ;
             foreach PL in PLs
             {
                  Medias = Medias_Build( PL.URL ) ;
                  foreach Media in Medias 
                              display Media image
             }
       }
 }

: 然而,我有一个用于 XML、图像等的异步加载模型,所以我想的是这样的:

Channels_Build("CHANNELS.XML");
Channels_Loaded()
{   // Channels build from some returned XML
    foreach Ch in Channels
        Cats_Build( Ch.URL, ??? ) ;   //async calls
}
Cats_Loaded()
{   // Cats build from some returned XML
    foreach Cat in Cats
        PLs_Build( Cat.URL ... ) ;
}
PLs_Loaded()
{   // PLs build from some returned XML
    foreach PL in PLs
        MediaList_Build( PL.URL ... ) ; 
}
MediaList_Loaded()
{   // MediaList build from some returned XML
    foreach media in MediaList
        display Media image 
}

每个 Channels_Build、Cats_Build、PLs_Build、M​​ediaList_Build 都会进行异步调用,因此有一个关联的回调 xxx_Loaded()

每个 Channel 有 1或更多类别。 每个类别有 1 个或多个播放列表。 每个播放列表有 1 个或多个媒体 因此,我有一个 4 层深的层次结构

。您可以假设 Channels、Cats、PL 和 MediaList 共享一个公共基类。

我应该将这个 4x 逻辑折叠到单个递归构建方法中吗?如何?我必须让构建过程将一些信息(父节点)传递给其相应的回调(我查找了 IAsyncResult.AsyncState)

我的大脑被锁定了,我想不出这里需要什么?递归?,将信息传递给异步调用,特定模式?,...

我应该首先将内存中的树数据构建到一个结构中吗?或者只有当我决定使用 TreeView 控件时才有用?例如,如果我决定使用重复模板将信息显示到列表框中,该怎么办?模板将显示:

Channel-Image+Name
        Category-Image+Name
                PlayList-Image+Name
                        MediaList images...

是的,非叶节点将在列表中视觉上重复。这很好,因为它可能会提供我正在寻找的地图。

所以令人困惑的问题仍然存在: 如何枚举并显示此异步模型中的所有节点?

谢谢。

Context Silverlight/WPF, C#, .NET 4,

I have a 4 level deep tree of thumbnails I need to enumerate and display in some meaningful way.
 For a Synchronous execution (sequential world) we could think like the following:

    Channels = Channels_Build("CHANNELS.XML");
    foreach Ch in Channels
    {
         Cats = Cats_Build( Ch.URL ) ;
         foreach Cat in Cats
         {
             PLs = PLs_Build( Cat.URL ) ;
             foreach PL in PLs
             {
                  Medias = Medias_Build( PL.URL ) ;
                  foreach Media in Medias 
                              display Media image
             }
       }
 }

.
However I have an Async loading model for XML, images, ..., so I am thinking something like this:

Channels_Build("CHANNELS.XML");
Channels_Loaded()
{   // Channels build from some returned XML
    foreach Ch in Channels
        Cats_Build( Ch.URL, ??? ) ;   //async calls
}
Cats_Loaded()
{   // Cats build from some returned XML
    foreach Cat in Cats
        PLs_Build( Cat.URL ... ) ;
}
PLs_Loaded()
{   // PLs build from some returned XML
    foreach PL in PLs
        MediaList_Build( PL.URL ... ) ; 
}
MediaList_Loaded()
{   // MediaList build from some returned XML
    foreach media in MediaList
        display Media image 
}

Each of Channels_Build, Cats_Build, PLs_Build, MediaList_Build make an Async call and thus have an associated callback xxx_Loaded()

Each Channel has 1 or more Categories.
Each Category has 1 or more PlayLists.
Each PlayList has 1 or more Media
Thus, I have a 4 levels deep hierarchical structure

You can assume Channels, Cats, PLs and MediaList share a common base class.

Should I fold this 4x logic into a single recursive build method? How? I would have to make the build process pass some info (parent node) to its corresponding callback (I looked up IAsyncResult.AsyncState)

My brain is locked up and I can't think of what's needed here? recursion?, passing info to Async calls, specific pattern?, ...

Should I build the tree data in memory, into one structure, first? or would that be useful only if I decide to use a TreeView control? What if I decide to display the info using a repeating template into a listbox for example. The template would display:

Channel-Image+Name
        Category-Image+Name
                PlayList-Image+Name
                        MediaList images...

Yes, the non-leaf nodes would repeat visually down the list. That's fine as it might provide the map I am looking for.

So the puzzling question remains:
How do I go about enumerating and displaying all the nodes in this Async model?

Thank you.

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

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

发布评论

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

评论(1

两仪 2024-11-15 20:34:11

我会看看异步 CTP 刷新,它可以大大简化这些事情

I would look at Async CTP refresh that simplifies this stuff a lot

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