Entity, BaseEntity and EntityMapperMixin - Feelynx/flutter_clean_architecture GitHub Wiki

BaseEntity Class

The BaseEntity class is an abstract class that represents an entity in the domain layer following the Clean Architecture pattern. Entities encapsulate core business logic and data relevant to the application’s domain. The BaseEntity class provides the foundational structure for all domain entities used within the application.

Entities interact with Data Transfer Objects (DTOs), typically by converting the entity into a DTO, and vice versa, allowing seamless data transfer between the domain and data layers.

  • Inherits from Equatable: By extending Equatable, the BaseEntity class provides value equality, ensuring that two instances of an entity can be compared based on their field values. This is essential for state management in Flutter to avoid unnecessary widget rebuilds when using entities as state.

  • Mixin EntityMapperMixin: The class includes the EntityMapperMixin, which provides a method to convert the entity into a Data Transfer Object (DTO). This mapping is crucial for the interaction between the domain and data layers, where entities (representing the business logic) must be converted into DTOs for communication with external data sources, such as APIs or databases.

Example Usage

A specific entity class would inherit from BaseEntity and define its fields and conversion behavior. For example, a User entity might extend BaseEntity and define user-specific fields such as name and email, along with the logic to convert the entity into a UserDTO.

class User extends BaseEntity<UserDTO> {
  final String name;
  final String email;

  const User({required this.name, required this.email});

  @override
  UserDTO toDTO() {
    return UserDTO(name: name, email: email);
  }

  @override
  List<Object?> get props => [name, email];
}

EntityMapperMixin

The EntityMapperMixin is a Dart mixin that provides the structure for converting an entity into a corresponding Data Transfer Object (DTO). This mixin ensures that each entity can be easily transformed into a DTO when required by the data layer.

  • Type parameter : The mixin uses a generic type to specify the type of DTO that the entity will map to. This allows flexibility in specifying the type of data object that the entity can be converted into.

  • toDTO() method: The toDTO() method is defined in the mixin but is left for implementation in the classes that mix it in. This method should return the DTO representation of the entity, making it ready for data transfer or storage.

Example Usage

When an entity class, such as User, mixes in EntityMapperMixin, it must implement the toDTO() method to provide the logic for converting the entity into its corresponding DTO, such as UserDTO.

mixin EntityMapperMixin<UserDTO> {
  UserDTO toDTO();
}

In this example, the toDTO() method is used to return a UserDTO from a User entity, facilitating data transfer to external systems.

Summary

The BaseEntity class provides a foundation for creating entities in the domain layer, ensuring value equality through Equatable and offering integration with the EntityMapperMixin for converting entities into DTOs. The EntityMapperMixin defines the structure for transforming entities into DTOs, leaving the specific implementation details to the entity classes themselves. These classes maintain a clear separation of concerns between the domain and data layers in a Clean Architecture, ensuring that the business logic (entities) remains distinct from data transfer mechanisms (DTOs).

⚠️ **GitHub.com Fallback** ⚠️