大型WASM:拥有州容器管理API呼叫是否违反了最佳实践?
在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论