简体   繁体   中英

Basic Java Service class which implements

There is a base service interface with the following methods:

public interface BaseService {

    Dto convertToDto(Entity entity);
    List<Dto> convertToDtoList(List<Entity> entityList);
    Entity convertToEntity(Dto dto);
    List<Entity> convertToEntityList(List<Dto> dtoList);

}

Now the methods convertToDtoList and convertToEntityList should get implemented in a base service implementation which is then extended by other services so that these methods only have to be implemented once in the base service. Both convertToDtoList and convertToEntityList have always the same implementation except they are using the different entity and dto types of the respective service class:

public List<Dto> convertToDtoList(List<Entity> entityList) {
    if (entityList == null)
        return null;

    List<Dto> dtoList = new ArrayList<Dto>();
    Iterator<Entity> it = entityList.iterator();

    while (it.hasNext())
        dtoList.add(this.convertToDto(it.next()));

    return dtoList;
}


public List<Entity> convertToEntityList(List<Dto> dtoList) {
    if (dtoList == null)
        return null;

    List<Entity> entityList = new ArrayList<Entity>();
    Iterator<Dto> it = dtoList.iterator();

    while (it.hasNext())
        entityList.add(this.convertToEntity(it.next()));

    return entityList;
}

How can I implement these methods in the base service in a generic way abstracting from the respective entity and dto types so that I can use them in every service class which is extending this base service?

You can use default implementation in interface and template <> :

public interface BaseService<D, E> {

    D convertToDto(E entity);

    E convertToEntity(D dto);

    default List<E> convertToEntityList(List<D> dtoList) {
        return Optional.ofNullable(dtoList).orElse(Collections.emptyList()).stream()
                       .map(this::convertToEntity)
                       .filter(Objects::nonNull)
                       .collect(Collectors.toList());
    }

    default List<D> convertToDtoList(List<E> entityList) {
        return Optional.ofNullable(entityList).orElse(Collections.emptyList()).stream()
                       .map(this::convertToDto)
                       .filter(Objects::nonNull)
                       .collect(Collectors.toList());
    }

}

public class DtoEntityBaseService implements BaseService<Dto, Entity> {

    @Override
    public Dto convertToDto(Entity entity) {}

    @Override
    public Entity convertToEntity(Dto dto) {}
}

You can look at very useful framework that help to do all these transformation. It calls Mapsruct

Do it as follows:

public interface BaseService <T> {

    Dto convertToDto(Entity entity);
    default public List<Dto> convertToDtoList(List<T> entityList) {
        if (entityList == null)
            return null;

        List<Dto> dtoList = new ArrayList<Dto>();
        Iterator<T> it = entityList.iterator();

        while (it.hasNext())
            dtoList.add(this.convertToDto(it.next()));

        return dtoList;
    }

    default public List<Entity> convertToEntityList(List<T> dtoList) {
        if (dtoList == null)
            return null;

        List<Entity> entityList = new ArrayList<Entity>();
        Iterator<T> it = dtoList.iterator();

        while (it.hasNext())
            entityList.add(this.convertToEntity(it.next()));

        return entityList;
    }
    Entity convertToEntity(Dto dto);
}

Check this for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM