Answer the question
In order to leave comments, you need to log in
Where would it be correct to place the conversion methods dto -> entity and vice versa?
I thought about several options.
1. Place directly in the dto in each.
2. Create a separate DTOUtils class and drop everything there (but then there will be a mix of methods for converting all classes)
3. Make a dtoutils package and create many classes there (for each dto its own, they will only store dto methods and that’s all)
3 option looks like the most prudent, but creating a separate class for one method is somehow not very good.
P,S, I heard about the mapstract, but the logic of turning into a DTO and vice versa is complicated. Need to work with DB.
Answer the question
In order to leave comments, you need to log in
Good afternoon!
Post directly to the dto in each.
Converter<S, T>
), then you need to create a class for each entity. Regarding the location of the packages, there are different practices. For example, put User, UserCreationDto, UserRepository, UserService, UserToDtoConverter, etc. into the user package. @Service
@RequiredArgsConstructor
public class MapperService {
private final ModelMapper modelMapper;
/**
* Note: outClass object must have default constructor with no arguments
*
* @param <D> type of result object.
* @param <T> type of source object to map from.
* @param entity entity that needs to be mapped.
* @param outClass class of result object.
* @return new object of <code>outClass</code> type.
*/
public <D, T> D map(final T entity, Class<D> outClass) {
return modelMapper.map(entity, outClass);
}
/**
* Note: outClass object must have default constructor with no arguments
*
* @param entityList list of entities that needs to be mapped
* @param outCLass class of result list element
* @param <D> type of objects in result list
* @param <T> type of entity in <code>entityList</code>
* @return list of mapped object with <code><D></code> type.
*/
public <D, T> List<D> mapAll(final Collection<T> entityList, Class<D> outCLass) {
return entityList.stream().map(entity -> map(entity, outCLass)).collect(Collectors.toList());
}
/**
* Maps {@code source} to {@code destination}.
*
* @param source object to map from
* @param destination object to map to
*/
public <S, D> D map(final S source, D destination) {
modelMapper.map(source, destination);
return destination;
}
}
Create a separate DTOUtils class and drop everything there (but then there will be a mix of methods for converting all classes)
Make a dtoutils package and create many classes there (for each dto its own, they will only store dto methods and that's it)
Option 3 looks like the most prudent, but creating a separate class for one method is somehow not very good.
Converter<S, T>
you are not catching up with the campaign what is dto and entity. Smart Western dudes name an entity with all sorts of business methods. But because of the annotations in the spring, people like you think that this is just a database model. No. And this is just a data model without methods, and this object is only needed to transfer data to another place.
conversion methods
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question