Creating a Repository Interface

Once the Entities and Failures have been created, we can proceed with the creation of the Note Repository which defines the functionalities of the note feature without providing any implementation details.

All repository methods must abide by these rules :

  • A repository method must always return an Either monad A repository method always returns an Either object which can denote a Left or Right value. If the method fails, then a Failure object (Left Value) will be returned. If the method succeeds, then a null, primitive object, or an Entity (Right Value) will be returned.

  • A stream return type must be wrapped by a VerboseStream A VerboseStream is a stream wrapper (from the codenic_bloc_use_case package) which enables us to convert any exceptions into Failure objects.

In the note_app/modules/domain/lib/note/repository_interfaces/ directory, create the note_repository.dart file:

import 'dart:async';

import 'package:codenic_bloc_use_case/codenic_bloc_use_case.dart';
import 'package:codenic_exception_converter/codenic_exception_converter.dart';
import 'package:domain/note/entities/note_entry.dart';
import 'package:domain/note/failures/note_content_too_long_failure.dart';
import 'package:domain/note/failures/note_title_too_long_failure.dart';

/// {@template NoteRepository}
/// A repository for managing [NoteEntry]s.
/// {@endtemplate}
abstract class NoteRepository {
  /// {@macro NoteRepository}
  const NoteRepository();

  /// Creates a new [NoteEntry] initialized the given [title] and [content].
  ///
  /// A [Failure] may be thrown:
  /// - [NoteContentTooLongFailure]
  /// - [NoteTitleTooLongFailure]
  FutureOr<Either<Failure, void>> createNoteEntry({
    String? title,
    String? content,
  });

  /// Updates the [NoteEntry] referenced by the given [id].
  ///
  /// A [Failure] may be thrown:
  /// - [NoteContentTooLongFailure]
  /// - [NoteTitleTooLongFailure]
  FutureOr<Either<Failure, void>> updateNoteEntry({
    required String id,
    String? title,
    String? content,
  });

  /// Deletes an existing [NoteEntry] with the given [id].
  FutureOr<Either<Failure, void>> deleteNoteEntry({required String id});

  /// Fetches a collection of [NoteEntry]s.
  ///
  /// The [limit] parameter can be used to limit the number of entries.
  /// The [pageToken] parameter can be used to fetch a specific page of entries.
  FutureOr<Either<Failure, List<NoteEntry>>> fetchNoteEntries({
    int? limit,
    dynamic pageToken,
  });

  /// Streams a collection of [NoteEntry]s.
  ///
  /// The [limit] parameter can be used to limit the number of entries.
  ///
  /// A [VerboseStream] is a [Stream] wrapper which gives it an error
  /// handler for converting [Exception]s into [Failure]s before it gets
  /// emitted
  FutureOr<Either<Failure, VerboseStream<Failure, List<NoteEntry>>>>
      watchNoteEntries({int? limit});
}

Last updated