A Runner is the most basic form of use case. It accepts a parameter, executes the task, then either returns a Left Value (error) or a Right Value (success).
In our case, an error is represented by the Failure object. The right value can either be null, a primitive object or an Entity.
To get started, we will create the following Runner use cases for the note app in the note_app/modules/domain/lib/note/use_cases/ directory:
CREATE USE CASE: Create a new note entry
Make a file named create_note_entry.dart and add the following code:
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/repositories/note_repository.dart';/// {@template CreateNoteEntryParams}/// The parameters for the [CreateNoteEntry] use case./// {@endtemplate}classCreateNoteEntryParamswithEquatableMixin {/// {@macro CreateNoteEntryParams}constCreateNoteEntryParams({required this.title, required this.content});/// The title of the note entry to create.finalString? title;/// The content of the note entry to create.finalString? content;@overrideList<Object?> get props => [title, content];}/// {@template CreateNoteEntry}/// See [NoteRepository.createNoteEntry]./// {@endtemplate}classCreateNoteEntryextendsRunner<CreateNoteEntryParams, Failure, void> {/// {@macro CreateNoteEntry}CreateNoteEntry({requiredNoteRepository noteRepository}): _noteRepository = noteRepository;/// A reference to the [NoteRepository] instance.finalNoteRepository _noteRepository;@overrideFutureOr<Either<Failure, void>> onCall(CreateNoteEntryParams params) => _noteRepository.createNoteEntry( title: params.title, content: params.content, );}
READ USE CASE: Fetch a collection of note entries
Make a file named fetch_note_entries.dart and add the following code:
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/repositories/note_repository.dart';/// {@template FetchNoteEntryParams}/// The parameters for the [FetchNoteEntries] use case./// {@endtemplate}classFetchNoteEntriesParamswithEquatableMixin {/// {@macro FetchNoteEntryParams}constFetchNoteEntriesParams({this.limit =10, this.pageToken});/// The number of [NoteEntry]s to fetch.finalint limit;/// A token used for fetching a particular page of [NoteEntry]s.finaldynamic pageToken;@overrideList<Object?> get props => [limit, pageToken];}/// {@template FetchNoteEntries}/// See [NoteRepository.fetchNoteEntries]./// {@endtemplate}classFetchNoteEntriesextendsRunner<FetchNoteEntriesParams, Failure, List<NoteEntry>> {/// {@macro FetchNoteEntry}FetchNoteEntries({requiredNoteRepository noteRepository}): _noteRepository = noteRepository;/// A reference to the [NoteRepository] instance.finalNoteRepository _noteRepository;@overrideFutureOr<Either<Failure, List<NoteEntry>>> onCall(FetchNoteEntriesParams params) => _noteRepository.fetchNoteEntries( limit: params.limit, pageToken: params.pageToken, );}
UPDATE USE CASE: Update note entry
Make a file named update_note_entry.dart and add the following code:
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/repositories/note_repository.dart';/// {@template UpdateNoteEntryParams}/// The parameters for the [UpdateNoteEntry] use case./// {@endtemplate}classUpdateNoteEntryParamswithEquatableMixin {/// {@macro UpdateNoteEntryParams}constUpdateNoteEntryParams({required this.id, this.title, this.content});/// The ID of the [NoteEntry] to update.finalString id;/// The new title of the [NoteEntry].finalString? title;/// The new content of the [NoteEntry].finalString? content;@overrideList<Object?> get props => [id, title, content];}/// {@template UpdateNoteEntry}/// See [NoteRepository.updateNoteEntry]./// {@endtemplate}classUpdateNoteEntryextendsRunner<UpdateNoteEntryParams, Failure, void> {/// {@macro UpdateNoteEntry}UpdateNoteEntry({requiredNoteRepository noteRepository}): _noteRepository = noteRepository;/// A reference to the [NoteRepository] instance.finalNoteRepository _noteRepository;@overrideFutureOr<Either<Failure, void>> onCall(UpdateNoteEntryParams params) => _noteRepository.updateNoteEntry( id: params.id, title: params.title, content: params.content, );}
DELETE USE CASE: Delete a note entry
Make a file named delete_note_entry.dart and add the following code:
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/repositories/note_repository.dart';/// {@template DeleteNoteEntryParams}/// The parameters for the [DeleteNoteEntry] use case./// {@endtemplate}classDeleteNoteEntryParamswithEquatableMixin {/// {@macro DeleteNoteEntryParams}constDeleteNoteEntryParams({required this.id});/// The ID of the [NoteEntry] to delete.finalString id;@overrideList<Object?> get props => [id];}/// {@template DeleteNoteEntry}/// See [NoteRepository.deleteNoteEntry]./// {@endtemplate}classDeleteNoteEntryextendsRunner<DeleteNoteEntryParams, Failure, void> {/// {@macro DeleteNoteEntry}DeleteNoteEntry({requiredNoteRepository noteRepository}): _noteRepository = noteRepository;/// A reference to the [NoteRepository] instance.finalNoteRepository _noteRepository;@overrideFutureOr<Either<Failure, void>> onCall(DeleteNoteEntryParams params) => _noteRepository.deleteNoteEntry(id: params.id);}