usecase_annotation 0.0.4 copy "usecase_annotation: ^0.0.4" to clipboard
usecase_annotation: ^0.0.4 copied to clipboard

usecase_annotation allows you to generate usecases class from the repository class

UseCase Annotation #

usecase_annotation allows you to generate usecases class from the repository class

It requires usecase_generator package for generating .uc.dart file

Add the package #

Add the usecase_annotation package as a dependencies and the usecase_generator package as a dev_dependencies as well as the build_runner package in your pubspec.yaml file:

dependencies:
  flutter:
    sdk: flutter
  usecase_annotation: latest

dev_dependencies:
  flutter_test:
    sdk: flutter
  build_runner: any
  usecase_generator: latest

Quick tutorial #

Create a auth_repo.dart class file and Annotate your repo class with @UseCase() to generate usecase classes for each functions:

NOTE: The repository class must be abstract!

@UseCase()
abstract class AuthRepository {
    void m1();
    Future<String> m2(int param1);
}

class AuthRepositoryImpl implements AuthRepository {
 // Concrete implementation
}

Run the build command:

flutter packages pub run build_runner build --delete-conflicting-outputs

This will generate theses classes in auth_repo.uc.dart file => M1UseCase, which calls authRepository.m1() M2UseCase, which calls authRepository.m2()

Important #

To have all the usecases files in same folder you need to create a build.yaml file next to pubspec.yaml file. In this file you need to add the below content:

targets:
    $default:
        builders:
            usecase_generator|usecase_gen:
                options:
                    build_extensions:
                        {
                            "^lib/domain/repositories/{{}}.dart": "lib/domain/usecases/{{}}.uc.dart",
                        }

You can update key and value pair according to your requirements

Motivation #

This package is build to follow Uncle bob clean architecture approach and create usecases classes for your repository classes.


Riverpod DI #

The package is build to support riverpod dependency injection. You need to add flutter_riverpod package to your pubspec.yaml file.

By default this package comes with riverpod dependency injection tu turn off riverpod di you need to make isInjectableDI parameter of UseCase annotation to true.



Injectable DI #

This package can also work with the injectable package.

Firstly you need to make isInjectableDI as true.

Then as all the usecases class ends with the -UseCase so you just need to create a build.yaml file next to pubspec.yaml file and In this file you need to add the below content:

targets:
  $default:
    builders:
      injectable_generator:injectable_builder:
        options:
          auto_register: true
          class_name_pattern: "UseCase$"

5
likes
160
points
161
downloads

Publisher

unverified uploader

Weekly Downloads

usecase_annotation allows you to generate usecases class from the repository class

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

More

Packages that depend on usecase_annotation