flutterblocflutter-blocfreezedflutter-freezed

How to conect entity(domain layer) and model(data layer) using BloC+Freezed


I'm trying to learn how to follow a clean feature-first architecture using Bloc and Freezed.

From what I've seen, I should have inside the Domain layer, an entity class declaring only its properties for the presentation layer to use, and in the Data layer, I should have a DTO of this class that's responsible for implementing the FromJson methods and whatever I might need when getting the data from the dataSources and parse it to an entity.

My questions are:

1- To which of these classes (if not both) do I create the freezed code?

2-How do I connect the entity and the model when using the repository, should I create "fromEntity, toEntity" methods or something alike? And if such methods were to be required, can I create them with Freezed?

I'm working with this entity class:

class NoteEntity{
  final String title;
  final String description;

  NoteEntity({required this.title, required this.description});
}

I was using it with freezed like this:

@freezed
class NoteEntity with _$NoteEntity {
      const factory NoteEntity({
        required String title,
        required String description,
      }) = _NoteEntity;
}

Solution

  • when we talk about an architecture, it will lead to decision making based on our needs in the application by following to the architecture pattern.

    1. freezed is a plugin to generate classes. like copyWith toString, operator == etc. so Basically you can use for both layer. you can generate the classes construtor without typing manually.

    again.... its about decision-making: in my case, I usually use the freezed only on domain layer. because i dont really need that on my data layer.


    1. yes, you can create manually fromEntity and toEntity in your domain model
    @freezed
    class NoteEntityDomain with _$NoteEntityDomain  {
          const factory NoteEntityDomain ({
            required String title,
            required String description,
          }) = _NoteEntity;
    
      // fromentity
      factory NoteEntityDomain.fromEntity(NoteEntity data) {
         return NoteEntityDomain(
           title: data.title,
           description: data.description
         )
       }
     // to entity method here
    }