DTO, BaseDTO and DTOMapperMixin - Feelynx/flutter_clean_architecture GitHub Wiki
The BaseDTO class is an abstract class that represents a Data Transfer Object (DTO) in the Clean Architecture. It provides a base structure for all DTOs used in the application.
DTOs are used to encapsulate data and transfer it between layers, such as from a remote server (data source) to the application domain layer.
-
Inherits from Equatable: By extending Equatable, the class provides value equality, allowing two instances of a subclass of BaseDTO to be compared based on their fields rather than their references. This is essential for ensuring consistency and avoiding unnecessary re-renders in Flutter when using state management.
-
Mixin DTOMapperMixin The class includes the DTOMapperMixin, which provides a method to map or convert the DTO into an entity object (of type I). Entities represent the core business objects in the domain layer, making this mapping important for translating data into a form usable by the domain layer.
A specific DTO class would inherit from BaseDTO and provide implementation details, including data fields and mapping behavior. For example, a UserDTO might extend BaseDTO and include user-specific fields (e.g., name, email) while implementing how it maps to a User entity.
class UserDTO extends BaseDTO<User> {
final String name;
final String email;
const UserDTO({required this.name, required this.email});
@override
User toEntity() {
return User(name: name, email: email);
}
@override
List<Object?> get props => [name, email];
}
The DTOMapperMixin is a Dart mixin that provides a mapping function for converting a DTO into its corresponding entity. In Clean Architecture, DTOs are often transformed into entities that belong to the domain layer, ensuring separation of concerns between the data and business logic.
-
Type parameter : The mixin uses a generic type to specify the type of entity that the DTO will map to. This allows flexibility in the types of entities the DTO can be converted into.
-
toEntity() method: The method toEntity() is defined, but not implemented in the mixin. The responsibility to implement this method falls on the classes that mix in DTOMapperMixin. It should return the entity (I) that corresponds to the specific DTO.
When a DTO class, such as UserDTO, mixes in DTOMapperMixin, it will implement the toEntity() method, providing the logic to convert the DTO into its corresponding domain entity, e.g., User.
mixin DTOMapperMixin<User> {
User toEntity();
}
In the above example, the toEntity() method would be used to return an instance of the User entity from a UserDTO.
BaseDTO serves as a foundational class for creating DTOs, providing equality checks and integration with the DTOMapperMixin to facilitate DTO-to-entity mapping. DTOMapperMixin defines the structure for converting DTOs into domain entities, leaving the implementation to the specific DTO classes. These classes work together within a Clean Architecture pattern, helping to maintain a clean separation between layers, where the data transfer layer (DTO) is independent of the domain/business logic layer (Entity).