mercredi 23 décembre 2020

Does DTOs comes under decorator pattern?

I have Entity and DTO classes defined in two different Entity and DTO packages as:

@Entity
@Table(name="Foo", schema="REF_DATA")
public class Foo extends BaseEntity {
 private Long fooId;
 private String fooName;
}


public class FooDto extends BaseDto {
 private Long fooId;
 private String fooName;
}

While returning values to the User, we are returning DTOs rather than Entity objects. For that we have declared the mapper and it's implementation as:

public interface DomainToDtoMapper<E extends BaseEntity, D extends BaseDto> {

   D mapDomainToDto(E domain);
}

public class FooDomainToDtoMapper implements DomainToDtoMapper<Foo, FooDto> {
    @Override
    public FooDto mapDomainToDto(Foo domain) {
        FooDto fooDto = new FooDto();
        FooDto.setfooId(domain.getfooId());
        FooDto.setfooName(domain.getfooName());
        return fooDto;
    }

}

And get list of all Foo objects call in the service layer looks something like:

@Override
public List<FooDto> getAllFoo() {
    return fooRepository.findAll().stream()
            .map(FooEntityDomainToDtoMapper::mapDomainToDto) // mapping entity to dto
            .collect(Collectors.toList());
}

My question is are we using Decorator pattern here? I know there is Data Transfer Object Pattern but since we are providing implementation for mapping, can we say it is decorator pattern? Or is there any other design pattern getting used here?

Aucun commentaire:

Enregistrer un commentaire