flutter - 为什么(通常)在 BLoC 模式上有一个存储库层?
问题描述
我是 Flutter 的新手,刚刚从阅读有关 Flutter 的教程中听说了 BLoC 概念。在本教程中,我第一次听说了 BLoC。但是我在这篇文章中也看到了一个名为“Repository”的文件。基本上数据流是这样的:
Web API --> Api Provider --> Repository --> BLoC --> Widget
我不明白的是,为什么需要 Repository 层,因为当我查看存储库文件时,它基本上只是返回 API Provider 的 Future 结果?我很好奇并尝试进一步搜索,我看到互联网上一些人的编码模式也有一个 Repository 层。
在原始文章中,API Provider 做了所有事情。它调用 get 请求,等待 Future 解析,将 JSON 数据转换为适当的模型,并返回 Future 包含的模型。
class ApiProvider {
Future<ItemModel> fetchMovieList() async {
final response = await client.get("http://api.themoviedb.org/3/movie/popular?api_key=$_apiKey");
if (response.statusCode == 200)
return ItemModel.fromJson(json.decode(response.body));
else
throw Exception('Failed to load post');
}
}
class Repository {
ApiProvider _api = ApiProvider();
Future<ItemModel> fetchMovieList() => _api.fetchMovieList(); // why?
}
class Bloc {
Repository _repository = Repository();
final _moviesFetcher = PublishSubject<ItemModel>();
Observable<ItemModel> get allMovies => _moviesFetcher.stream;
fetchAllMovies() async {
ItemModel itemModel = await
_repository.fetchAllMovies();
_moviesFetcher.sink.add(itemModel);
}
}
目前我对其进行了修改,以便 Api 提供者返回纯 Future,其中存储库实现.then()
并将响应转换为适当的数据,但我倾向于避免等待,因为在 React Native 中等待会导致应用程序看起来没有响应。我还将错误检查移到 BLoC 中。
class ApiProvider {
Future fetchMovieList() => client.get("http://api.themoviedb.org/3/movie/popular?api_key=$_apiKey");
}
class Repository {
ApiProvider _api = ApiProvider();
Future<ItemModel> fetchMovieList() => _api.fetchMovieList().then(response => ItemModel.fromJson(json.decode(response.body));
}
class Bloc {
Repository _repository = Repository();
final _moviesFetcher = PublishSubject<ItemModel>();
Observable<ItemModel> get allMovies => _moviesFetcher.stream;
fetchAllMovies() async => _repository.fetchPopularMovies().then((response) => _moviesFetcher.sink.add(response))
.catchError((onError) => throw Exception("Failed to load post $onError"));
}
但是,我仍然觉得这是证明需要这个存储库层的一个延伸。如果可以的话,我想让它像这样:
class ApiProvider {
Future<ItemModel> fetchMovieList() => client.get("http://api.themoviedb.org/3/movie/popular?api_key=$_apiKey")
.then(response => ItemModel.fromJson(json.decode(response.body));
}
class Bloc {
ApiProvider _api = ApiProvider();
final _moviesFetcher = PublishSubject<ItemModel>();
Observable<ItemModel> get allMovies => _moviesFetcher.stream;
fetchAllMovies() async => _api.fetchPopularMovies().then((response) => _moviesFetcher.sink.add(response))
.catchError((onError) => throw Exception("Failed to load post $onError"));
}
并完全摆脱存储库层。我并不是说存储库层是不必要的,但现在我不知道存储库层试图解决什么模式问题。我只想知道为什么首先有一个存储库层,以及存储库在现实世界中的重要用例是什么。我知道这个问题可能会被标记为可以引发讨论而不是直接回答的问题。但我相信这个问题有某种狭窄的答案。当我尝试在互联网上搜索时,我只是找不到它(搜索结果与“存储库”术语的其他用途混在一起,如 git 和颠覆)。
解决方案
好吧,算了。我发现这篇优秀的文章解释了 Repository 基本上是抽象数据的来源,无论是来自磁盘缓存、云还是其他来源。工厂将根据每个来源的可用性决定使用哪种来源。来电者只需要通过一扇门。因为上面的教程只有一个来源(API/云),所以当时我觉得它没用。
推荐阅读
- javascript - node.js express 服务器在队列中保持获取内存
- google-sheets - 谷歌表格循环依赖问题与if函数
- php - 如何在 PHP 中的接口实现中指定参数
- mongodb - 图像未显示在反应传单弹出窗口(MongoDB)中
- reactjs - React - 根据条件显示地图中的第一项
- multithreading - [zephyr-rtos][riot-os] Zephyr vs. RIOT OS
- android - Android,constraintLayout 与 constraintDimensionRatio 适合屏幕(或其他约束布局)
- python - 遍历列表列表并删除不需要的字符串
- r - DataExplorer 无法创建 PDF 报告
- report - 有没有办法在 tablix 和页面末尾之间显示一个字符串?