示例工程 ch16-bookmarks
ch16-bookmarks/lib/src/domain/bookmark_repository.dart
/// 仓储接口:应用层只依赖这个抽象,不依赖 drift / http。
///
/// 对照 Go:接口 + 实现注入;对照 C:函数指针表。Riverpod 的 override 就是注入手段。
library;
import 'bookmark.dart';
abstract interface class BookmarkRepository {
/// 响应式列表:数据一变就推新值(drift 的 watch)。
Stream<List<Bookmark>> watchAll();
Future<int> add(BookmarkDraft draft);
Future<void> setFavorite(int id, {required bool favorite});
Future<void> delete(int id);
/// 从远程导入若干条,返回实际写入的数量。
Future<int> importFromRemote({int limit = 10});
}
/// 远程数据源接口:实现可以是 http、可以是为测试写的假实现。
abstract interface class BookmarkRemoteApi {
Future<List<BookmarkDraft>> fetchDrafts({int limit = 10});
}
/// 同步失败(网络/解析),带用户可读文案。
class SyncException implements Exception {
const SyncException(this.message, {this.cause});
final String message;
final Object? cause;
@override
String toString() => 'SyncException: $message';
}