大型WASM:拥有州容器管理API呼叫是否违反了最佳实践?

发布于 2025-02-07 07:07:55 字数 756 浏览 0 评论 0原文

在Blazor Wasm中,您说您有一个管理事物列表的州容器。在整个应用程序中,该状态容器被注入多个剃须刀组件。不用响应一个统一的状态容器并从每个组件中调用适当的API,而只是请求状态容器中的事物列表,如果未初始化事物列表,则状态容器本身本身称合适的API获取它们?类似:

    public class ThingState
    {
        private IEnumerable<Thing> Things { get; set; }

        public async IEnumerable<Thing> GetThings()
        {
            if (Things == null)
            {
                Things = await Http.GetFromJsonAsync<IEnumerable<Thing>>("ThingsAPI");
                NotifyStateChanged();
            }

            return Things;
        }

        public event Action OnChange;

        private void NotifyStateChanged() => OnChange?.Invoke();

    }

我已经看到的样本通常是在剃须刀组件上进行管理的,但是如果有多个使用相同状态容器的组件,那似乎是重复的。这是实施此功能的不好方法吗?

In Blazor WASM, say you have a state container that manages a list of Things. This state container is injected into multiple Razor Components throughout the application. Is it bad to, instead of responding to the state container being unitialized and calling the appropriate API from each component, to just request the list of the Things from the state container, and if the list of Things is not initialized, the state container itself calls the appropriate API to fetch them? Something like:

    public class ThingState
    {
        private IEnumerable<Thing> Things { get; set; }

        public async IEnumerable<Thing> GetThings()
        {
            if (Things == null)
            {
                Things = await Http.GetFromJsonAsync<IEnumerable<Thing>>("ThingsAPI");
                NotifyStateChanged();
            }

            return Things;
        }

        public event Action OnChange;

        private void NotifyStateChanged() => OnChange?.Invoke();

    }

Samples I have seen generally manage this at the razor component, but that seems duplicative if there are multiple components that use the same state container. Is this a bad way to implement this?

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

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

发布评论

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