requestDto vs command

์ง€ํ˜œยท2025๋…„ 2์›” 13์ผ
0

๐Ÿ’ก https://github.com/cjh0412/Schedule_jpa

์ด๋ฒˆ์— ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด์„œ ํŠœํ„ฐ๋‹˜๊ป˜ ํŠน์ • ์ž‘์—…์„ ์ˆ˜ํ–‰ํ• ๋•Œ requestDto์˜ ๊ฐ’์„ ๊ทธ๋Œ€๋กœ ๋„˜๊ธฐ์ง€ ๋ง๊ณ  command์— ๋‹ด์•„์„œ ๋„˜๊ธฐ๋Š” ๊ฒƒ์ด ๋” ์ข‹๋‹ค๋Š” ํ”ผ๋“œ๋ฐฑ์„ ๋ฐ›์•˜๋‹ค.
์ฒ˜์Œ ํ”ผ๋“œ๋ฐฑ์„ ๋ฐ›์•˜์„๋•Œ๋Š” reqeustDto์™€ command๋ฅผ ๋‚˜๋ˆ„๋Š” ๊ตฌ๋ถ„์ด ์ž˜ ์ดํ•ด๊ฐ€์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹น ๋ถ€๋ถ„์„ ์ •๋ฆฌํ•ด๋ณด๊ณ ์ž ํ•œ๋‹ค.

1. reqeustDto vs command

๐Ÿ“ reqeustDto

  • ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„๋กœ ์š”์ฒญ๋ณด๋‚ธ ๋ฐ์ดํ„ฐ ์ „์†ก ๊ฐ์ฒด
  • ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋ณด๋‚ธ ๋ฐ์ดํ„ฐ(email, password ๋“ฑ)์„ ์บก์Šํ™” ํ•˜๋Š” ์—ญํ• 
  • ์™ธ๋ถ€์—์„œ ๋„˜์–ด์˜ค๋Š” ๋ฐ์ดํ„ฐ๋งŒ ํฌํ•จ

์˜ˆ์‹œ)

// ์ „๋‹ฌ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๋Š” ์—ญํ• 
@Getter
public class CommentRequestDto {
    private  Long todoId;
    private String content;
}

๐Ÿ“ command

  • ํŠน์ • ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ช…๋ น์„ ๋‚˜ํƒ€๋ƒ„
  • createCommentCommand๋ผ๋Š” ํด๋ž˜์Šค์˜ ๊ฒฝ์šฐ ์ƒˆ๋กœ์šด ๋Œ“๊ธ€์„ ์ƒ์„ฑํ•˜๋Š” ์ž‘์—…์— ๋Œ€ํ•œ ๋ช…๋ น์„ ์˜๋ฏธ
  • ํ•ด๋‹น ๋ช…๋ น์˜ ์ฒ˜๋ฆฌ๋Š” service์—์„œ ๋‹ด๋‹นํ•˜์—ฌ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์‹คํ–‰
  • Comment ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•œ todoId(์ผ์ • ๊ณ ์œ  ์‹๋ณ„์ž), content ๊ฐ™์€ ๊ฐ’ ์ด์™ธ์— ๋น„์ฆˆ๋‹ˆ์Šค ๊ทœ์น™(ex ๋Œ“๊ธ€์€ 10์ž ์ด์ƒ ๋“ฑ)์ด๋‚˜ ์—ฐ์‚ฐ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ์ฒด

๐Ÿ“Œ ์˜ˆ์‹œ)

// ์–ด๋–ค์‚ฌ์šฉ์ž๊ฐ€ ์–ด๋–ค ์ผ์ •์— ์–ด๋–ค ๋Œ“๊ธ€์„ ์ž‘์„ฑํ•  ๊ฒƒ์ธ์ง€ ๋“ฑ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จ

@Getter
@AllArgsConstructor
public class CreateCommentCommand {
    private  Long todoId;
    private String content;
    private Long memberId;
}

๐Ÿค” ์™œ ํ•„์š”ํ• ๊นŒ?

  • ๊ฐ ๊ณ„์ธต๋ณ„(Controller, Service)์˜ ๊ธฐ๋Šฅ์ด ๋”์šฑ ๋ช…ํ™•ํ•ด์ง
  • command๋Š” ์‹ค์ œ ์„œ๋น„์Šค์—์„œ ์‚ฌ์šฉํ•  ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ, requestDto๋Š” ๋ฐ์ดํ„ฐ ๊ตํ™˜์„ ์œ„ํ•ด ํ•„์š”ํ•œ ๊ตฌ์กฐ

2. requestDto๋งŒ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด?

command์™€ dto์˜ ๊ตฌ๋ถ„์ด ์–ด๋Š์ •๋„ ์ดํ•ด๋Š” ๋˜์—ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ๋งŒ์•ฝ requestDto๋งŒ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์–ด๋–ค ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธธ๊นŒ?

์—ญํ• ์˜ ๋ถ„๋ฆฌ๊ฐ€ ๋ช…ํ™•ํ•˜์ง€ ์•Š์Œ

  • RequestDto๋Š” ํด๋ผ์ด์–ธํŠธ์™€ ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ ๋ฐ›๊ธฐ ์œ„ํ•œ ๊ฐ์ฒด
  • Command๋Š” ์„œ๋น„์Šค ๋กœ์ง์„ ์œ„ํ•œ ๋ช…๋ น ๊ฐ์ฒด
    โŒ ํ˜ผ์šฉํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ฒŒ ๋  ๊ฒฝ์šฐ ๋‘ ๊ฐ€์ง€ ์—ญํ• ์ด ์„ž์ด๊ฒŒ ๋จ

์„œ๋น„์Šค ๊ณ„์ธต์—์„œ ์ถ”๊ฐ€ ๋ฐ์ดํ„ฐ๊ฐ€ ํ•„์š”ํ•  ๊ฒฝ์šฐ ๋ฌธ์ œ ๋ฐœ์ƒ

requestDto๋งŒ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ, ์„œ๋น„์Šค ๊ณ„์ธต์—์„œ requestDto์˜ ๋ฐ์ดํ„ฐ ๋ฟ๋งŒ์•„๋‹ˆ๋ผ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ๊ฐ’์ด ํ•„์š”ํ•  ๊ฒฝ์šฐ ๋ณ„๋„๋กœ ๊ด€๋ฆฌํ•ด์ค˜์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ฝ”๋“œ๊ฐ€ ์ง€์ €๋ถ„ํ•ด ์งˆ ์ˆ˜ ์žˆ์Œ

๐Ÿ“Œ ์˜ˆ์‹œ)

// requestDto๋งŒ ์‚ฌ์šฉํ•œ ๊ฒฝ์šฐ
// memberId ๋ณ„๋„๋กœ ๊ด€๋ฆฌํ•ด์•ผํ•จ
public CommentResponseDto save(Long memberId, CommentRequestDto commentRequestDto) {
    Member findMember = memberRepository.getReferenceById(memberId);
    Todo todo = todoRepository.findById(commentRequestDto.getTodoId())
            .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "ํ• ์ผ ์ •๋ณด๋ฅผ ๋‹ค์‹œ ์กฐํšŒํ•ด ์ฃผ์„ธ์š”"));

    Comment comment = new Comment(commentRequestDto.getContent(), findMember, todo);
    commentRepository.save(comment);

    return new CommentResponseDto(comment.getId(),
            comment.getTodo().getId(),
            comment.getContent(),
            comment.getMember().getId(),
            comment.getMember().getUsername(),
            comment.getCreatedAt());
}


// command๋ฅผ ๋ถ„๋ฆฌํ•œ ๊ฒฝ์šฐ
public CommentResponseDto save(CreateCommentCommand command) {
    Member findMember = memberRepository.getReferenceById(command.getMemberId());
    Todo todo = todoRepository.findById(command.getTodoId())
            .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "ํ• ์ผ ์ •๋ณด๋ฅผ ๋‹ค์‹œ ์กฐํšŒํ•ด ์ฃผ์„ธ์š”"));

    Comment comment = new Comment(command.getContent(), findMember, todo);
    commentRepository.save(comment);

    return CommentResponseDto.toDto(comment);
}

์œ ์ง€๋ณด์ˆ˜์™€ ํ™•์žฅ์˜ ์–ด๋ ค์›€

  • ์ถ”ํ›„ ๊ธฐ๋Šฅ์ด ์ถ”๊ฐ€๋œ๋‹ค๋ฉด dto์˜ ๋กœ์ง์ด ์ ์  ์ปค์ง€๊ณ  ๋ณต์žกํ•ด์งˆ ๊ฐ€๋Šฅ์„ฑ์ด ์กด์žฌํ•จ
  • Command๋กœ ๋ณ„๋„ ๊ด€๋ฆฌํ•˜๋ฉด ๊ฐ๊ฐ์˜ ์—ญํ• ์„ ๋ถ„๋ฆฌํ•˜์—ฌ ๊ด€๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ฝ”๋“œ ๋ณ€๊ฒฝ์ด ์šฉ์ด

๐Ÿ’ก ๊ฒฐ๋ก 

๋”ฐ๋ผ์„œ ํ•ด๋‹น ํด๋ž˜์Šค๋กœ dto์™€์˜ ์—ญํ™œ์„ ๋ถ„๋ฆฌํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์˜ ์—ญํ• ์„ ๋ช…ํ™•ํ•˜๊ฒŒ ๊ตฌ๋ถ„ํ•ด์ฃผ๋ฉฐ, ์ดํ›„ ์œ ์ง€๋ณด์ˆ˜ ์ธก๋ฉด์—์„œ๋„ ๋„์›€์ด ๋˜๋ฏ€๋กœ ๋ณ„๋„๋กœ ์ƒ์„ฑํ•˜์—ฌ ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ๋ฐ”๋žŒ์งํ•˜๋‹ค!



๊ทธ์™ธ ํŠธ๋Ÿฌ๋ธ” ์ŠˆํŒ…

  1. static ๋ฉ”์„œ๋“œ ํ™œ์šฉํ•˜๊ธฐ
  1. ํ”„๋กœ์ ํŠธ ์˜ค๋ฅ˜ ์ˆ˜์ •

0๊ฐœ์˜ ๋Œ“๊ธ€

๊ด€๋ จ ์ฑ„์šฉ ์ •๋ณด