如何在 Silverlight/CSLA 中创建延迟加载和缓存的集合

发布于 2024-07-21 00:04:20 字数 1196 浏览 10 评论 0原文

我正在为使用 CSLA 编写的现有桌面应用程序创建 Silverlight 前端。 我遇到麻烦的一件事是转换如下所示的类:

public class SomeCollection : Csla.ReadOnlyListBase<SomeCollection, SomeObject>
{
    private static SomeCollection _list = null;
    public static SomeCollection GetSomeCollection()
    {
        if (_list == null)
        {
            _list = DataPortal.FetchChild<SomeCollection>();
        }
        return _list;
    }
}

代码中充满了“SomeCollection.GetSomeCollection()” 这在 silverlight 端不起作用,因为所有 DataPortal 访问都是异步的,因此您必须从如下所示:

public static void GetSomeCollection(EventHandler<DataPortalResult<SomeCollection>> callback)
{
    DataPortal<SomeCollection> portal = new DataPortal<SomeCollection>();
    portal.FetchCompleted += callback;
    portal.BeginFetch();
}

当数据准备好时,回调处理程序被调用。 我当然可以缓存此结果,但与此同时任何 SomeCollection.GetSomeCollection() 调用都会失败。

我尝试阻塞直到异步调用完成,但到目前为止我还没有运气。 这不是一个很好的解决方案,但我不知道如果在加载数据之前调用 SomeCollection.GetSomeCollection() 还能做什么。 我能想到的唯一其他选择是允许 SomeCollection.GetSomeCollection() 返回 null,然后以某种方式将所有调用者转换为处理 null 返回值

有什么想法吗?

(我对 Silverlight 和 Csla 非常陌生,所以我可能会以完全错误的方式处理这个问题)

I'm creating a Silverlight front end for an existing desktop app written using CSLA. One thing that I'm having trouble with is converting classes like the following:

public class SomeCollection : Csla.ReadOnlyListBase<SomeCollection, SomeObject>
{
    private static SomeCollection _list = null;
    public static SomeCollection GetSomeCollection()
    {
        if (_list == null)
        {
            _list = DataPortal.FetchChild<SomeCollection>();
        }
        return _list;
    }
}

The code is peppered with "SomeCollection.GetSomeCollection()" This won't work on the silverlight side because all DataPortal access is asyncronous, so you have to start with something like the following:

public static void GetSomeCollection(EventHandler<DataPortalResult<SomeCollection>> callback)
{
    DataPortal<SomeCollection> portal = new DataPortal<SomeCollection>();
    portal.FetchCompleted += callback;
    portal.BeginFetch();
}

The callback handler gets called when the data is ready. I can certainly cache the result of this, but in the meantime any SomeCollection.GetSomeCollection() calls will fail.

I've tried blocking until the asynchronous call completes, but I've had no luck so far. That's not a great solution, but I don't know what else to do if SomeCollection.GetSomeCollection() is called before the data has been loaded. the only other option I can think of is to allow SomeCollection.GetSomeCollection() to return null, and then somehow convert all callers to handle null return values

Any thoughts?

(I'm super new to Silverlight and Csla, so it's possible that I'm going about this the completely wrong way)

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文