-
Notifications
You must be signed in to change notification settings - Fork 154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
๐ 2๋จ๊ณ - ๋ฆฌํฉํฐ๋ง(๋ฉ๋ด) #101
base: dacapolife87
Are you sure you want to change the base?
Conversation
- MenuName, MenuPrice, MenuGroupName ์ถ์ถ - ํจํค์ง ์ด๋
- ๊ฐ ๊ฐ์ฒด ํ ์คํธ์ฝ๋ ์์ฑ
- Dto ๊ฐ์ฒด ์ฌ์ฉ์ผ๋ก Entity๋ถ๋ฆฌ
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ฉ๋ด ๋ฆฌํฉํ ๋ง ์ํด์ฃผ์
จ์ต๋๋ค ๐
๋ช๊ฐ์ง ์๊ฐ๊ฑฐ๋ฆฌ๋ฅผ ๋จ๊ฒจ๋์๋๋ฐ, ์ง๋ฌธ์ด ์๋ค๋ฉด ํธํ๊ฒ DM ์ฃผ์ธ์ ๐
columnDefinition = "varbinary(16)", | ||
foreignKey = @ForeignKey(name = "fk_menu_to_menu_group") | ||
) | ||
private MenuGroup menuGroup; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MenuGroup Aggregate๋ฅผ ์ง์ ์ฐธ์กฐ๋ก ๊ตฌํํ ์ด์ ๊ฐ ์์๊น์ ?
columnDefinition = "varbinary(16)", | ||
foreignKey = @ForeignKey(name = "fk_menu_product_to_menu") | ||
) | ||
private List<MenuProduct> menuProducts; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MenuProduct ์ผ๊ธ ์ปฌ๋ ์ ์ ๋ง๋ค์ด ์ฌ์ฉํด๋ณด์ธ์ ๐
) | ||
private List<MenuProduct> menuProducts; | ||
|
||
public Menu() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JPA Entity์ ๊ธฐ๋ณธ ์์ฑ์๋ protected ์ ๊ทผ์ ์ด์๋ฅผ ์ ์ธํ๋๊ฒ ์ด๋จ๊น์ ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/next-step/ddd-tactical-design/pull/101/files#r850475902 ๋ฅผ ํ์ธํด์ฃผ์ธ์ :)
public Menu() { | ||
} | ||
|
||
public Menu(PurgomalumClient purgomalumClient, String name, BigDecimal price, MenuGroup menuGroup, boolean displayed, List<MenuProduct> menuProducts) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Menu๋ผ๋ Aggregate ๋ ์ ์๋ ๋ถ๋ณ์(๋ฉ๋ด ๊ฐ๊ฒฉ์ ์ํด์๋ ์ํ์ ๊ฐ๊ฒฉ๋ณด๋ค ์๊ฑฐ๋ ๊ฐ๋ค. ๋ฑ)์ ๋ชจ๋ ๋ง์กฑํด์ผ ํฉ๋๋ค.
์ด๋ค ๋ถ๋ณ์๋ค์ด ์กด์ฌํ๊ณ , ๋๋ฉ์ธ ๋ก์ง์ ์ด๋ค ๋ฐฉ๋ฒ์ผ๋ก ๋
น์ผ์ ์๋์ง ๊ณ ๋ฏผํด๋ณด์ธ์.
|
||
} | ||
|
||
public void show() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ฉ๋ด๋ฅผ ๋ ธ์ถํ๊ธฐ ์ํ ์๊ตฌ์ฌํญ์ ๋ง์กฑ์ํค์ง ๋ชปํ ๊ฑฐ ๊ฐ์์. ๐
foreignKey = @ForeignKey(name = "fk_menu_product_to_product") | ||
name = "product_id", | ||
columnDefinition = "varbinary(16)", | ||
foreignKey = @ForeignKey(name = "fk_menu_product_to_product") | ||
) | ||
private Product product; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Product ๋๋ฉ์ธ์ ์ง์ ์ฐธ์กฐ๋ก ๊ตฌํํ ์ด์ ๊ฐ ์์๊น์ ?
์๋ก ๋ค๋ฅธ ์ปจํ
์คํธ์ Aggregate๊ฐ ๊ฒฐํฉ๋๋ฅผ ๋ฎ์ถ๋ค๋ฉด ์ด๋ค ์ฅ์ ์ด ์์์ง ๊ณ ๋ฏผํด๋ณด์ธ์ ๐
if (Objects.isNull(price) || price.compareTo(BigDecimal.ZERO) < 0) { | ||
throw new IllegalArgumentException(); | ||
} | ||
public MenuResponse create(final MenuRequest request) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์์ง ์ค์ํ ๋น์ฆ๋์ค ๋ก์ง์ด ์๋น์ค ๋ ์ด์ด์ ๋
ธ์ถ๋์ด ์๋๊ฒ ๊ฐ์์.
๋ง์ฝ ํ์ฌ ๊ตฌ์กฐ์์ ๋ค๋ฅธ ๊ฐ๋ฐ์๋ถ์ด ์๋น์ค ๋ ์ด์ด์ ์๋ ๋ก์ง์ ๋ณด์ง๋ชปํ๊ณ Menu ๋๋ฉ์ธ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค๋ฉด Menu ๊ฐ์ฒด๋ ์์ ํ ๊ฐ์ฒด๋ผ๊ณ ํ ์ ์์๊ฒ ๊ฐ์์ ๐ข
๋
ธ์ถ๋์ด ์๋ ๋น์ฆ๋์ค ๋ก์ง์ ๋๋ฉ์ธ ๋ด๋ถ๋ก ์ฎ๊ฒจ๋ณด์ธ์ ๐
|
||
@Table(name = "menu") | ||
@Entity | ||
public class Menu { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์๋กญ๊ฒ ์ ์ํ ํด๋์ค๋ค์ ํ
์คํธ ์ฝ๋๊ฐ ํ์ํด๋ณด์ฌ์.
domain ํจํค์ง์ ์กด์ฌํ๋ ํด๋์ค๋ค์ ๋จ์ํ
์คํธ๋ฅผ ํ์๋ก ์์ฑํด๋ณด์ธ์ ๐
์๋
ํ์ธ์ ๋ฆฌ๋ทฐ์ด๋ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ํผ๋๋ฐฑ ๋ฐ์ ์ํด์ฃผ์
จ๋ค์ ๐
๋ช๊ฐ์ง ์๊ฐ๊ฑฐ๋ฆฌ๋ฅผ ์ถ๊ฐ๋ก ๋จ๊ฒจ๋์์ด์.
์ง๋ฌธ์ด ์์ผ๋ฉด ์ธ์ ๋ ์ง DM ์ฃผ์ธ์ ๐
import java.util.Objects; | ||
|
||
@Embeddable | ||
public class Price { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Value Ojbect์ ๋ถ๋ณ์ฑ, ๊ฐ ๋๋ฑ์ฑ์ ์ํด Equals & HashCode ๋ฅผ ์ถ๊ฐํด๋ณด๋๊ฑด ์ด๋จ๊น์ ?
public static Price add(Price totalPrice, Price addPrice) { | ||
return new Price(totalPrice.price.add(addPrice.price)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public static Price add(Price totalPrice, Price addPrice) { | |
return new Price(totalPrice.price.add(addPrice.price)); | |
} | |
public Price add(Price addPrice) { | |
return new Price(this.price.add(addPrice.price)); | |
} |
static ๋ฉ์๋๋ฅผ ์ ๊ณตํ๊ธฐ ๋ณด๋ค๋ ๊ฐ์ฒด์ ๋ฉ์์ง๋ฅผ ๋์ง์ ์๋๋ก ์ํ๊ฐ์ ์ด์ฉํ๋๊ฒ์ด ์ข์๋ณด์ฌ์ ๐
} | ||
menu.setPrice(price); | ||
|
||
List<Product> findProducts = productRepository.findAllByIdIn(menu.getProductIds()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Product ๋๋ฉ์ธ์ด MenuContext์ ์กด์ฌํ์ง ์๋๋ก ๋๋ฉ์ธ ์๋น์ค
๋ฅผ ๊ตฌํํด๋ณด๋๊ฑด ์ด๋จ๊น์ ?
) | ||
private List<MenuProduct> menuProducts; | ||
|
||
public Menu() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/next-step/ddd-tactical-design/pull/101/files#r850475902 ๋ฅผ ํ์ธํด์ฃผ์ธ์ :)
public Menu() { | ||
} | ||
|
||
public Menu(PurgomalumClient purgomalumClient, UUID id, String name, Price price, UUID menuGroupId, boolean displayed, MenuProducts menuProducts) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์์ฑ์๊ฐ ๋ค์ํด์ง๋๊ฒฝ์ฐ ์ฃผ / ๋ถ ์์ฑ์ ๊ฐ๋
์ ๋์
ํ์ฌ ๋ณด๋๊ฑด ์ด๋จ๊น์ ?
https://jackjeong.tistory.com/148
@Embedded | ||
private MenuGroupName name; | ||
|
||
public MenuGroup() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/next-step/ddd-tactical-design/pull/101/files#r850475902
์ ์ฝ๋ฉํธ๋ฅผ ํ์ธ๋ถํ๋๋ ค์
private void validation(String name) { | ||
if (Objects.isNull(name) || name.isEmpty()) { | ||
throw new IllegalArgumentException("๋ฉ๋ด๊ทธ๋ฃน์ ์ด๋ฆ์ Null๋๋ ๊ณต๋ฐฑ์ด ๋ ์ ์์ต๋๋ค."); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ฉ๋ด ๊ทธ๋ฃน ์ด๋ฆ๊ณผ ๊ฐ์ ๋น์ฆ๋์ค ์๊ตฌ์ฌํญ์ MenuGroupName VO๊ฐ ๋ด๋นํ๋๊ฑด ์ด๋จ๊น์ ?
) | ||
private Product product; | ||
@Column(name = "product_id", columnDefinition = "varbinary(16)", nullable = false) | ||
private UUID productId; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ฉ๋ด ๊ฐ๊ฒฉ๊ณผ ๊ด๋ จ๋ ๋ถ๋ณ์ฑ์ ์งํค๊ธฐ ์ํ์ฌ ํฌ๊ฒ 2๊ฐ์ง ์ ๋์ ๋ฐฉ์์ด ์์๊ฒ ๊ฐ์์.
- MenuProduct์์ Product์ ๊ฐ๊ฒฉ ์ ๋ณด๋ฅผ ๊ฐ์ง๊ณ ์๋๋ค.
- MenuProduct์์ ๋๋ฉ์ธ ์๋น์ค๋ฅผ ํตํ์ฌ Product์ ๊ฐ๊ฒฉ Validation์ ์งํํ๋ค.
2๊ฐ์ง ๋ฐฉ๋ฒ ๋ชจ๋ ๋ง์ด ์ฌ์ฉ๋๊ณ , ์ฅ๋จ์ ์ด ์์ด์.
์ฅ๋จ์ ์ ๋ํ์ฌ ๊ณ ๋ฏผํด๋ณด์๊ณ ๋ฏธ์
์งํ ํด๋ณด์ธ์ ๐
์ด๋ฒ ๋ฏธ์
์์ ๊ฐ์ฅ ์ค์ํ ๋ถ๋ถ์ด๋ผ๊ณ ์๊ฐํ์ฌ ์กฐ๊ธ์ ๋๋ฃจ๋ญ์คํ๊ฒ ์ฝ๋ฉํธ๋ฅผ ๋จ๊ฒจ๋๋ฆด๊ฒ์.
๊ตฌํ์ ๋ํ์ฌ ๊ถ๊ธ์ฆ์ด ์์ผ์ค๋๋ ํธํ๊ฒ Slack DM ๋ถํ๋๋ฆด๊ฒ์ !
import kitchenpos.common.domain.Price; | ||
import kitchenpos.menus.ui.dto.MenuProductRequest; | ||
import kitchenpos.products.tobe.domain.Product; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋๋ฉ์ธ ์ฝ๋์์๋ ์ธ๋ถ Context๊ฐ ๊ฒฐํฉ๋๊ฐ ์์ผ๋ฉด ์ข์๊ฒ ๊ฐ์์. ๐
import kitchenpos.menus.ui.dto.MenuProductRequest; | ||
import kitchenpos.products.tobe.domain.Product; | ||
|
||
import javax.persistence.*; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ํจํค์ง ๋ด์ ๋ชจ๋ ํด๋์ค๋ฅผ importํ ํ์๊ฐ ์์๊น์ ?
https://google.github.io/styleguide/javaguide.html#s3.3.1-wildcard-imports
์๋
ํ์ธ์ ๋ฆฌ๋ทฐ์ด๋ ๋ถ์กฑํ ๋ถ๋ถ์ด๋ ์๋ชป๋์ ์์ผ๋ฉด ๋ง์ ์ง์ ๋ถํ๋๋ฆฌ๊ฒ ์ต๋๋ค |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ฆฌ๋ทฐ๊ฐ ๋ง์ด ๋ฆ์ด์ ธ ์ฃ์กํฉ๋๋ค. ๐
์ง๋ฌธ์ฃผ์ ๋ด์ฉ์ ๋ํ ๋ต๋ณ์ ๋จ๊ฒจ๋์๋๋ฐ ํ์ธ ๋ถํ๋๋ ค์ !
this.menuProducts = menuProducts; | ||
} | ||
|
||
public MenuProducts(List<MenuProductDto> menuProductDtos, List<Product> products, Price menuPrice) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋๋ฉ์ธ ์๋น์ค ๊ตฌํ์ ๊ดํด ์ด๋ ค์์ ๊ฒช๊ณ ์๋๊ฒ ๊ฐ์ ์ ๊ฐ ๊ตฌํํ๋ ๋ฐฉ๋ฒ์ ์๊ฐ ์์ผ๋๋ฆด๊ฒ์.
์ฐ์ ์์ ์ด์ผ๊ธฐ ๋๋ฆฐ๊ฒ์ฒ๋ผ Aggreagte๊ฐ ๊ฐ์ ์ฐธ์กฐ๋ฅผ ์ด์ฉํ๋ฉด์ ์๋ก ๋ค๋ฅธ Context๊ฐ์ ๊ฒฐํฉ์ ๋ฎ์ถ๊ธฐ์ํด ๋๋ฉ์ธ์๋น์ค๋ฅผ ์ฌ์ฉํ๊ณค ํด์.
์๋ ์ฝ๋๋ ์์์ด๋ ์ฐธ๊ณ ๋ง ๋ถํ๋๋ ค์ ๐
MenuProducts ๋ฅผ ์์ฑํ ๋ ์ํ์ด ์กด์ฌํ๋์ง, ๊ฐ๊ฒฉ์ด ์ ํจํ์ง ํ๋จํ๊ธฐ ์ํด์ ์๋์ ๊ฐ์ด ๊ตฌํํ ์ ์์๊ฒ ๊ฐ์์.
public MenuProducts(List<MenuProductDto> menuProductDtos, MenuCreateService menuCreateService, Price menuPrice) {
menuCreateService.validate(menuProductDtos);
...
}
public class MenuCreateService {
private final ProductRepository productRepository;
public void validate(List<MenuProductDto> menuProductDtos) {
menuProductDtos.forEach(dto -> {
Product product = productRepository.findById(dto.getProductId());
// ๋ฉ๋ด ๊ฐ๊ฒฉ๊ณผ ๊ด๋ จ๋ ์ ํจ์ฑ ๊ฒ์ฌ ์คํ
})
}
}
๊ฐ์ธ์ ์ผ๋ก๋ ์์ ๊ฐ์ด ์์ฑ์ ๋๋ ๋ฉ์๋ ํ๋ผ๋ฏธํฐ๋ก ๋๋ฉ์ธ ์๋น์ค๋ฅผ ์ ๋ฌ ๋ฐ์ ๋๋ฉ์ธ ์๋น์ค ๋ด๋ถ์์ ์ธ๋ถ Context์ ๋๋ฉ์ธ๊ณผ ํ๋ ฅํ์ฌ ์๊ตฌ์ฌํญ์ ๊ตฌํํ๋ ๋ฐฉ๋ฒ์ ์ ํธํฉ๋๋ค.
์ ์ฝ๋์์๋ Repository ๋ฅผ ์ฃผ์ ๋ฐ์์ง๋ง ์ด์ ๊ฐ์ ํํ๋ ๋์ผํ ํ๋ก์ ํธ์์ ํจํค์ง ๋จ์๋ก ์ปจํ ์คํธ๊ฐ ๊ตฌ๋ถ๋์ด ์์๋ ์ฌ์ฉํ ์ ์๋ ๋ฐฉ๋ฒ์ด์์.
๋ง์ดํฌ๋ก ์๋น์ค๋ก ๊ตฌํ๋์ด ์๋ ํ๋ก์ ํธ์์๋ ๊ฐ ํ๋ก์ ํธ๊ฐ ๋ฌผ๋ฆฌ์ ์ผ๋ก ๋ถ๋ฆฌ๋์ด ์๊ธฐ ๋๋ฌธ์ Repository๊ฐ ์๋ ์ธ๋ถ Client๋ฅผ ์ฌ์ฉํ๊ฒ ๋๊ฒ ์ฃ .
์์ฒ๋ผ ๊ตฌํํ๋๊ฒ์ ๋ํ ์ฅ์ ์ Aggregate / Service ๋ ์ด์ด๋ ๋ค๋ฅธ Context์ ๊ฒฐํฉ์ด ์์ด์ง๋ฉฐ ๊ตฌ์กฐ๊ฐ ๋ณ๊ฒฝ๋๋ ๊ฒฝ์ฐ (๋ชจ๋๋ฆฌ์์ํคํ ์ฒ์์ ๋ง์ดํฌ๋ก์๋น์ค๋ก ๋ถ๋ฆฌ๋๋ ๊ฒฝ์ฐ)์๋ ๋๋ฉ์ธ์๋น์ค์์ ์ฃผ์ ๋ฐ๋ Repository์ ๊ตฌํ์ฒด๋ฅผ ๋ณ๊ฒฝํด์ฃผ๋๊ฒ์ผ๋ก ๋ง์ดํฌ๋ก ์๋น์ค๋ก์ ๋ถ๋ฆฌ๊ฐ ๊ฐ๋ฅํ๋ค๋๊ฒ์ด์์.
์ฐ์ ์์ฒ๋ผ ๊ตฌํํ๋๊ฒ์ ๋์ ํด๋ณด์ธ์ !
์๋ ํ์ธ์ ๋ฆฌ๋ทฐ์ด๋
2๋จ๊ณ ๋ฉ๋ด ๋ฆฌํฉํ ๋ง ์งํํ์์ต๋๋ค.
์ผ๋จ ์ ์ฒด์ ์ผ๋ก ๋ฆฌํฉํ ๋ง์ด ์งํ์ด ์๋๋ผ
๋ฉ๋ด ๋ถ๋ถ๋ง ๋ฆฌํฉํ ๋ง์ ์งํํ๋ค๋ณด๋ ์๋น์ค๋ ์ด์ด๊ฐ ์ ๋ฆฌ๊ฐ ๋ ๋ ๊ธฐ๋ถ์ด ๋๋ค์
์ต๋ํ ๋ฉ๋ด๊ด๋ จ๋ ๋ถ๋ถ ๋๋ฉ์ธ ๋ด๋ถ๋ก ์ฎ๊ฒจ์ ์ฒ๋ฆฌ๊ฐ ๋๋๋ก ์์ ํด๋ณด์์ต๋๋ค.
์ด๋ฒ์๋ ๋ง์ ์ง์ ๊ณผ ํผ๋๋ฐฑ ๋ถํ๋๋ฆฌ๊ฒ ์ต๋๋ค
๊ฐ์ฌํฉ๋๋ค.