DevKim

[02] μƒν’ˆ 도메인 개발 λ³Έλ¬Έ

Spring Boot

[02] μƒν’ˆ 도메인 개발

on_doing 2021. 7. 8. 21:31
728x90

πŸƒ κ΅¬ν˜„ κΈ°λŠ₯πŸƒ

1.μƒν’ˆ 등둝

2. μƒν’ˆ λͺ©λ‘ 쑰회

3. μƒν’ˆ μˆ˜μ •


[ μƒν’ˆ 엔티티에 λΉ„μ¦ˆλ‹ˆμŠ€ 둜직 μΆ”κ°€ ]

- μƒν’ˆ 엔티티에 주문에 λ”°λ₯Έ, 재고 μˆ˜λŸ‰μ„ 늘리고 쀄여보고 μ˜ˆμ™Έμ²˜λ¦¬λ₯Ό λ°œμƒ

 

도메인 주도 μ„€κ³„μ‹œ, μ—”ν‹°ν‹° μžμ²΄μ—μ„œ ν•΄κ²°ν•  수 μžˆλŠ” 것듀은 μ—”ν‹°ν‹° μ•ˆμ— λΉ„μ¦ˆλ‹ˆμŠ€ λ‘œμ§μ„ λ„£λŠ”κ²Œ 더 μ’‹κ³ ,

응집도가 있고 객체지ν–₯적이닀.

 

πŸ“Œ1. 재고 μˆ˜λŸ‰ μΆ”κ°€

πŸ“Œ2. 재고 μˆ˜λŸ‰ κ°μ†Œ ( μˆ˜λŸ‰μ΄ 0보닀 μž‘μ„ μ‹œ, 였λ₯˜κ°€ λ°œμƒν•΄μ•Όν•œλ‹€ )

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "dtype")
@Getter @Setter
public class Item {

    ...

    /**
     * todo : λΉ„μ¦ˆλ‹ˆμŠ€ 둜직
     * 1. 재고 μˆ˜λŸ‰ μΆ”κ°€
     * 2. 재고 μˆ˜λŸ‰ κ°μ†Œ (0보닀 μž‘μ„ μ‹œ, 였λ₯˜ λ°œμƒ)
     */

    public void addStock(int quantity)
    {
        this.stockQuantity+=quantity;
    }

    public void removeStock(int quantity)
    {
        int restStock=this.stockQuantity-quantity;
        if(restStock<0)
        {
            throw new NotEnoughStockException("need more stock");
        }
        this.stockQuantity=quantity;
    }

}

μˆ˜λŸ‰μ΄ λ‚˜κ°”μ„ 경우,

전체 재고λ₯Ό λΉΌμ£ΌλŠ”λ° μ΄λ•Œ λ°œμƒν•˜λŠ” μ˜ˆμ™Έμ²˜λ¦¬λŠ” RuntimeException을 μƒμ†λ°›μ•„μ„œ λ”°λ‘œ μƒμ„±ν•˜μ˜€λ‹€

public class NotEnoughStockException extends RuntimeException {
    public NotEnoughStockException() {
        super();
    }

    public NotEnoughStockException(String message) {
        super(message);
    }

    public NotEnoughStockException(String message, Throwable cause) {
        super(message, cause);
    }

    public NotEnoughStockException(Throwable cause) {
        super(cause);
    }

    protected NotEnoughStockException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }
}

[ μƒν’ˆ Repository ]

- μƒν’ˆ 등둝

- μƒν’ˆ 쑰회(단건,전체)

@Repository
@RequiredArgsConstructor
public class ItemRepository {

    private final EntityManager em;

    //μ €μž₯
    public void save(Item item) {
        if (item.getId() == null) {
            em.persist(item);
        } else {
            em.merge(item);
        }
    }
    ...
}

null이 μ•„λ‹ˆλΌλ©΄, μ€€μ˜μ† μƒνƒœμ˜ μ—”ν‹°ν‹°μ΄λ―€λ‘œ 병합(merge)을 μ‚¬μš©ν•˜μ—¬ μ—”ν‹°ν‹°λ₯Ό μ˜μ† μƒνƒœλ‘œ λ³€κ²½ν•œλ‹€.


[ μƒν’ˆ μ„œλΉ„μŠ€ 개발 ]

- μƒν’ˆ μ„œλΉ„μŠ€λŠ” μƒν’ˆ repositoryλ₯Ό λ‹¨μˆœ μœ„μž„λ§Œν•˜λŠ” 클래슀 정도이닀.

 

 Todo 

μƒν’ˆ μ—…λ°μ΄νŠΈμ‹œ μ˜λ―ΈμžˆλŠ” λ©”μ†Œλ“œ ν•˜λ‚˜ λ§Œλ“€μ–΄μ„œ price,name..λ“± νŒŒλΌλ―Έν„°λ‘œ λ„˜κ²¨μ„œ μ—…λ°μ΄νŠΈν•˜λŠ” μ½”λ“œλ‘œ λ¦¬νŒ©ν† λ§

( set 도배 X ) 

 

728x90
Comments