Skip to content

Repositories

Dustin Catap edited this page Jun 19, 2024 · 2 revisions

The purpose of a repository is to abstract the data access layer, providing a clean and consistent API for accessing data from various sources, such as a database, network, or in-memory storage.

For example, the following repository interface defines a method for getting a post by its ID:

class PostRepository {
  final PostRemoteDataSource _postRemoteDataSource;
  final PostLocalDataSource _postLocalDataSource;
  final PostMapper _postMapper;
  final ConnectivityService _connectivityService;

  PostRepository(
    this._postRemoteDataSource,
    this._postLocalDataSource,
    this._postMapper,
    this._connectivityService,
  );

  @override
  Future<PostEntity> getPost(int id) async {
    final bool isConnected = await _connectivityService.isConnected();

    if (isConnected) {
      final PostDataContract contract = await _postRemoteDataSource.getPost(id);
      final PostDataObject dataObject = _postMapper.mapObject<PostDataContract, PostDataObject>(contract);
      await _postLocalDataSource.addOrUpdate(dataObject);
    }

    final PostDataObject? dataObject = await _postLocalDataSource.getPost(id);
    final PostEntity entity = _postMapper.mapObject<PostDataObject, PostEntity>(dataObject);

    return entity;
  }
}
  • We have two data sources: PostRemoteDataSource and PostLocalDataSource.
  • PostRemoteDataSource is responsible for fetching data from an external source (like an API).
  • PostLocalDataSource is responsible for fetching data from a local source (like a database).
  • We check if the remote data source can be reached using ConnectivityService and checking if there is an Internet connection.
  • If the remote data source can be reached, we fetch the post from the API.
  • If not, we fetch the post from the local data source.
  • Both of the returned values of each data source will be mapped to our domain entity PostEntity using our domain mapper PostMapper.
  • We abstract the implementation from the caller. It doesn't need to know where the data is coming from.