Skip to content

Domain Layer

The core business entities and their relationships are defined, which is the heart of the application. It's also the layer that defines the use cases of the application.

To follow this step you have to understand :

Structure

  • entities Entities are used to encapsulate the data and behavior of the business objects and to define the relationships between them.
  • repositories The repository pattern provides a way to encapsulate the data access logic and to decouple the business logic from the data storage mechanism.
  • usecases The use cases are used to encapsulate the business logic of the application and to define the specific actions that the user can perform, such as creating, reading, updating, or deleting data.

1. Create Entitas

User Entity

class UserEntity extends Equatable {
  final int id;
  final String name;
  final String username;
  final String email;
  final String phone;
  final String website;
  final UserAddressEntity address;

  const UserEntity({
    required this.id,
    required this.name,
    required this.username,
    required this.email,
    required this.phone,
    required this.website,
    required this.address,
  });

  static const empty = UserEntity(
    id: 0,
    name: '',
    username: '',
    email: '',
    phone: '',
    website: '',
    address: UserAddressEntity.empty,
  );

  bool get isEmpty => this == UserEntity.empty;

  bool get isNotEmpty => this != UserEntity.empty;

  @override
  List<Object> get props {
    return [
      id,
      name,
      username,
      email,
      phone,
      website,
      address,
    ];
  }
}

User Params Entity

class UserParamsEntity extends Equatable {
  final int limit;
  final int start;
  final bool isInit;

  const UserParamsEntity({
    this.limit = 20,
    this.start = 1,
    this.isInit = true
  });

  @override
  List<Object> get props => [limit, start, isInit];
}

Info

After class Entity made this class can be extended to Model in the data package.

2. Setup Repositories

abstract class UserRepository {
  Future<Either<Failure, List<UserEntity>>> getUserData(UserParamsEntity params);
}

Info

After the abstract class created, you can implement the function in the data package.

3. Create Use Cases

Once the Entities and Repositories are created you can proceed to this step.

class UserGetData extends UseCase<List<UserEntity>, UserParamsEntity> {
  final UserRepository userRepository;

  UserGetData(this.userRepository);

  @override
  Future<Either<Failure, List<UserEntity>>> call(UserParamsEntity params) async {
    Either<Failure, List<UserEntity>> data = await userRepository.getUserData(params);

    return data.fold(
      (failure) => Left(failure),
      (value) => Right(value),
    );
  }
}

4. Register to Dependancy Injection

After dont forget to UserGetData created please register on injection.dart in Feature Layer.

sl.registerLazySingleton(() => UserGetData(sl()));

For more information injectable.

Authors: Nanang Prasetya