数据层
一个关于实现 MVVM 架构的应用程序的数据层的逐步讲解。
应用程序的数据层,在 MVVM 术语中被称为模型,是所有应用程序数据的真实来源。作为真实来源,它是唯一应该更新应用程序数据的地方。
它负责从各种外部 API 消费数据,将数据暴露给 UI,处理需要更新数据的 UI 事件,并在需要时向这些外部 API 发送更新请求。
- 仓库是应用程序数据的真实来源,包含与该数据相关的逻辑,例如响应新的用户事件更新数据或从服务轮询数据。仓库负责在支持离线功能时同步数据、管理重试逻辑和缓存数据。
- 服务是无状态的 Dart 类,与 API 交互,例如 HTTP 服务器和平台插件。应用程序需要的任何不在应用程序代码本身中创建的数据都应从服务类中获取。
定义服务
#服务类是所有架构组件中最明确的。它是无状态的,并且它的函数没有副作用。它的唯一工作是封装外部 API。通常,每个数据源都有一个服务类,例如客户端 HTTP 服务器或平台插件。
例如,在 Compass 应用程序中,有一个 APIClient 服务,用于处理与面向客户端服务器的 CRUD 调用。
class ApiClient {
// Some code omitted for demo purposes.
Future<Result<List<ContinentApiModel>>> getContinents() async { /* ... */ }
Future<Result<List<DestinationApiModel>>> getDestinations() async { /* ... */ }
Future<Result<List<ActivityApiModel>>> getActivityByDestination(String ref) async { /* ... */ }
Future<Result<List<BookingApiModel>>> getBookings() async { /* ... */ }
Future<Result<BookingApiModel>> getBooking(int id) async { /* ... */ }
Future<Result<BookingApiModel>> postBooking(BookingApiModel booking) async { /* ... */ }
Future<Result<void>> deleteBooking(int id) async { /* ... */ }
Future<Result<UserApiModel>> getUser() async { /* ... */ }
}
服务本身是一个类,其中每个方法封装不同的 API 端点并暴露异步响应对象。继续前面的删除已保存预订的例子,deleteBooking 方法返回一个 Future<Result<void>>。
定义仓库
#仓库的唯一职责是管理应用程序数据。仓库是单一类型应用程序数据的真实来源,并且应该是该数据类型被修改的唯一位置。仓库负责从外部来源轮询新数据、处理重试逻辑、管理缓存数据以及将原始数据转换为领域模型。
对于应用程序中的每种不同类型的数据,你应该有一个单独的仓库。例如,Compass 应用程序具有名为 UserRepository、BookingRepository、AuthRepository、DestinationRepository 等的仓库。
以下示例是 Compass 应用程序中的 BookingRepository,展示了仓库的基本结构。
class BookingRepositoryRemote implements BookingRepository {
BookingRepositoryRemote({
required ApiClient apiClient,
}) : _apiClient = apiClient;
final ApiClient _apiClient;
List<Destination>? _cachedDestinations;
Future<Result<void>> createBooking(Booking booking) async {...}
Future<Result<Booking>> getBooking(int id) async {...}
Future<Result<List<BookingSummary>>> getBookingsList() async {...}
Future<Result<void>> delete(int id) async {...}
}
BookingRepository 将 ApiClient 服务作为输入,用于从服务器获取和更新原始数据。重要的是,该服务是一个私有成员,以便 UI 层无法绕过仓库并直接调用服务。
使用 ApiClient 服务,仓库可以轮询服务器上可能发生的已保存预订的更新,并发出 POST 请求以删除已保存的预订。
仓库转换为应用程序模型的原始数据可以来自多个来源和多个服务,因此仓库和服务之间存在多对多的关系。一个服务可以被任何数量的仓库使用,并且一个仓库可以使用多个服务。
领域模型
#BookingRepository 输出 Booking 和 BookingSummary 对象,这些是领域模型。所有仓库都输出相应的领域模型。这些数据模型与 API 模型不同,因为它们只包含应用程序所需的数据。API 模型包含原始数据,通常需要过滤、组合或删除才能对应用程序的视图模型有用。仓库完善原始数据并将其输出为领域模型。
在示例应用程序中,领域模型通过方法(如 BookingRepository.getBooking)上的返回值暴露。getBooking 方法负责从 ApiClient 服务获取原始数据,并将其转换为 Booking 对象。它通过组合来自多个服务端点的数据来做到这一点。
// This method was edited for brevity.
Future<Result<Booking>> getBooking(int id) async {
try {
// Get the booking by ID from server.
final resultBooking = await _apiClient.getBooking(id);
if (resultBooking is Error<BookingApiModel>) {
return Result.error(resultBooking.error);
}
final booking = resultBooking.asOk.value;
final destination = _apiClient.getDestination(booking.destinationRef);
final activities = _apiClient.getActivitiesForBooking(
booking.activitiesRef);
return Result.ok(
Booking(
startDate: booking.startDate,
endDate: booking.endDate,
destination: destination,
activity: activities,
),
);
} on Exception catch (e) {
return Result.error(e);
}
}
完成事件循环
#在整个页面中,你已经看到用户如何删除已保存的预订,从一个事件开始——用户在 Dismissible 部件上滑动。视图模型通过将实际的数据修改委托给 BookingRepository 来处理该事件。以下代码段显示了 BookingRepository.deleteBooking 方法。
Future<Result<void>> delete(int id) async {
try {
return _apiClient.deleteBooking(id);
} on Exception catch (e) {
return Result.error(e);
}
}
仓库使用 _apiClient.deleteBooking 方法向 API 客户端发送 POST 请求,并返回一个 Result。HomeViewModel 消费 Result 及其包含的数据,然后最终调用 notifyListeners,完成循环。
反馈
#由于本网站的这一部分正在不断发展,我们欢迎你的反馈!