arch-atlas

Fetch newest books use case

Domain Layer

02:05

Summary

This lesson demonstrates the practical benefits of the abstract UseCase class by quickly creating a new FetchNewestBooksUseCase. By duplicating the previously built featured books use case and modifying the class name and repository method, the instructor highlights how standardizing use cases speeds up development. It reinforces the Clean Architecture principle that domain layer use cases should be uniform, predictable, and strictly scoped to a single repository interaction.

Key Ideas

  • UseCases follow a highly standardized structure when extending a generic base UseCase class.
  • Creating new UseCases becomes a rapid process of duplicating boilerplate and adjusting the specific repository method call.
  • The call method signature remains identical across different UseCases, ensuring uniform invocation from the presentation layer.
  • Predictable code patterns in the domain layer lower the cognitive load for developers navigating the codebase.
  • FetchNewestBooksUseCase calls the fetchNewestBooks method from HomeRepo, continuing the pattern of delegating data fetching to the repository abstraction.

Code Snippets

FetchNewestBooksUseCase implementationdart
import 'package:dartz/dartz.dart';

import '../../../../core/errors/failure.dart';
import '../../../../core/use_cases/use_case.dart';
import '../entities/book_entity.dart';
import '../repos/home_repo.dart';

class FetchNewestBooksUseCase extends UseCase<List<BookEntity>, NoParam> {
  final HomeRepo homeRepo;

  FetchNewestBooksUseCase(this.homeRepo);

  @override
  Future<Either<Failure, List<BookEntity>>> call([NoParam? param]) async {
    return await homeRepo.fetchNewestBooks();
  }
}

My Notes

The primary takeaway from this extremely short lesson is the power of consistency in Clean Architecture. Once you define a generic UseCase<Type, Param>, adding new features feels mundane—which is exactly the goal.

  • Gotcha: When copy-pasting use cases, it is extremely easy to forget to update the repository method call (e.g., leaving fetchFeaturedBooks() inside FetchNewestBooksUseCase). Always double-check the inner method invocation.
  • [Editor's note] While manually copying files takes "seconds" as the instructor notes, creating a custom snippet in VS Code for UseCase generation is even faster and less prone to human error.
  • Future Impact: This uniformity will pay off immensely when integrating with Bloc/Cubit, as the presentation layer will consume every UseCase in the exact same manner.