DTO в объект и наоборот. Какой уровень между контроллером и службой должен обрабатывать преобразование?JAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 DTO в объект и наоборот. Какой уровень между контроллером и службой должен обрабатывать преобразование?

Сообщение Anonymous »


I usually implement methods within DTOs to convert them into entities. I do this because I aim to keep the code of entities as clean as possible. However, I received feedback suggesting that it's more natural for an entity to accept a DTO as a parameter and create the entity. I read the article how to convert DTO to/from Entity, but I didn't understand it well even when I read it.

DTO

public record MemberRegistRequestDto( @NotBlank String oauthId, @NotBlank String oauthPlatform, @NotBlank String name, @NotBlank String profileImg, @NotBlank String nickname, @NotNull int birth, @NotBlank String gender, @NotBlank String profession, @NotBlank String signatureColor, @NotBlank String contentType ) { public Member of() { return Member.builder() .oauthId(oauthId) .oauthPlatform(getOAuthProviderFromString(oauthPlatform)) .name(name) .profileImg(profileImg) .nickname(nickname) .birth(birth) .gender(gender) .profession(profession) .signatureColor(signatureColor) .profileImg(profileImg) .build(); } ... } ENTITY

@Entity @Getter @Builder @AllArgsConstructor @NoArgsConstructor(access = AccessLevel.PUBLIC) @Table(indexes = @Index(name = "oauth_id", columnList = "oauthId", unique = true)) public class Member extends BaseTimeEntity implements UserDetails { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(nullable = false) private String oauthId; @Enumerated(EnumType.STRING) private OAuthProvider oauthPlatform; @Column(nullable = false) private String name; private String profileImg; @Column(nullable = false) private String nickname; private Integer birth; private String gender; private String profession; @Column(nullable = false) private String signatureColor; // 로그인 정보 식별 값, 프로필 사진, 필명, 이름, 성별, 생년월일, 이메일 @OneToMany(mappedBy = "member", cascade = CascadeType.ALL) private List adates; ... } Convert Dto to Entity (Service Layer)

@Transactional public void registMember(MemberRegistRequestDto memberRegistRequestDto) { Member member = memberRegistRequestDto.of(); memberRepository.save(member); } The suggestion to have the entity accept the DTO as a parameter.

var member = Member.of(memberRegistRequestDto);
  • Which layer is best? Controller or Service I implemented the pattern where the controller passes the DTO to the service and receives it back. This is because I see the controller as the layer that receives input from the user and passes it on to the service.
I've read many posts on Stack Overflow, but I haven't been able to understand them well.

Which i've read clean coder blog

Create Data Transfer Objects


Источник: https://stackoverflow.com/questions/780 ... the-servic
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»