Domain Layer
While adding new features, we will always start with the domain layer. The domain layer contains the entities and repositories/contracts of the feature. The domain layer is independent of the other layers and can be tested separately.
Getting started
Create a new folder named domain
inside the scratch_notes
folder.
Entities
- Create a new file named
scratch_note.dart
inside thedomain
folder. This file will contain the entity class for the feature.
import 'package:freezed_annotation/freezed_annotation.dart';
part 'scratch_note.freezed.dart';
part 'scratch_note.g.dart';
@freezed
class ScratchNote with _$ScratchNote {
const factory ScratchNote({
required String id,
required String userId,
required String title,
required DateTime createdAt,
}) = _ScratchNote;
factory ScratchNote.fromJson(Map<String, dynamic> json) =>
_$ScratchNoteFromJson(json);
factory ScratchNote.empty() => _ScratchNote(
id: '',
userId: '',
title: '',
createdAt: DateTime.now(),
);
}
We're using the freezed
package to create immutable classes. The ScratchNote
class has four properties: id
, userId
, title
, and createdAt
. We also have a factory constructor to create an empty ScratchNote
.
Repositories
Create a new file named i_scratch_note_repository.dart
inside the domain
folder. This file will contain the repository interface for the feature.
import 'package:fpdart/fpdart.dart';
import 'package:techshoi_app/features/core/domain/entities/failure.dart';
import 'package:techshoi_app/features/scratch_notes/domain/scratch_note.dart';
/// An abstract class representing the interface for a scratch note repository.
///
/// This interface defines the methods for interacting with scratch notes.
abstract class IScratchNoteRepository {
/// Retrieves a list of scratch notes.
///
/// Returns a [Future] that completes with an [Either] containing a [Failure]
/// or a list of [ScratchNote] objects.
Future<Either<Failure, List<ScratchNote>>> getScratchNotes();
/// Adds a new scratch note.
///
/// Returns a [Future] that completes with an [Either] containing a [Failure]
/// or [Unit].
Future<Either<Failure, Unit>> addScratchNote(String title);
/// Updates an existing scratch note.
///
/// Returns a [Future] that completes with an [Either] containing a [Failure]
/// or [Unit].
Future<Either<Failure, Unit>> updateScratchNote(ScratchNote note);
/// Deletes a scratch note.
///
/// Returns a [Future] that completes with an [Either] containing a [Failure]
/// or [Unit].
Future<Either<Failure, Unit>> deleteScratchNote(String noteId);
/// Listens to changes in the scratch notes.
///
/// Returns a [Stream] that emits an [Either] containing a [Failure]
/// or a list of [ScratchNote] objects.
Stream<Either<Failure, List<ScratchNote>>> listenToScratchNotes();
}
The IScratchNoteRepository
interface defines the methods for interacting with scratch notes. The methods include getting, adding, updating, and deleting scratch notes. We also have a method to listen to changes in the scratch notes. We're using the fpdart
package to handle errors. The Failure
class , defined in the core
module, is a class that represents a failure.