Learn
Getting started
Build a typed DataSourceContainer and a Repository that publishes domain data.
v0.1.0-dev.1Beginnerark_data_layer
Getting started
Install
shell
dart pub add ark_data_layer
Declare a source contract
dart
abstract interface class UserRemoteDataSource implements DataSource {
Future<List<UserDto>> fetchUsers();
}
DataSource is a marker. The application owns the useful contract and keeps it focused on one technical responsibility.
Implement the Repository
dart
final class UserRepositoryImpl extends Repository<UsersData>
implements UserRepository {
UserRepositoryImpl({required super.dataSources})
: _remote = dataSources.get<UserRemoteDataSource>(),
super(initialData: const UsersData.empty());
final UserRemoteDataSource _remote;
@override
Future<void> refresh() async {
final records = await _remote.fetchUsers();
setData(UsersData(records.map(mapUser)));
}
}
The concrete class extends the package base and implements an application-owned domain interface. Callers depend on UserRepository, not on DataSourceContainer.
Compose and close
dart
final repository = UserRepositoryImpl(
dataSources: DataSourceContainer(<DataSource>[remoteSource]),
);
await repository.refresh();
await repository.close();
await remoteSource.close();
The Repository closes its own publication mechanism. It does not close a scope-owned DataSource.