์นดํ…Œ๊ณ ๋ฆฌ ์—†์Œ

Controller, Service, Repository ๋ž€?

suesoo 2023. 8. 17. 01:57

๐Ÿ”ฅ Controller, Service, Repository๋ฅผ ์•Œ์•„๋ณด๊ธฐ ์ „์— 3๊ฐ€์ง€ ์š”์†Œ๋“ค์ด ์†ํ•œ MVC ํŒจํ„ด์ด ์ดํ•ด๋˜์–ด์•ผ ํ•œ๋‹ค(์ •ํ™•ํžˆ๋Š” Model)

MVCํŒจํ„ด

โ–ถ MVCํŒจํ„ด์ด๋ž€?

- MVC๋Š” Model, View, Controller์˜ ์•ฝ์ž์ด๋ฉฐ, ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜, ํ”„๋กœ์ ํŠธ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ์š”์†Œ๋ฅผ ์„ธ ๊ฐ€์ง€ ์—ญํ• ๋กœ ๊ตฌ๋ถ„

- ์‚ฌ์šฉ์ž๊ฐ€ ์›น ๋ธŒ๋ผ์šฐ์ €๋ฅผ ํ†ตํ•ด URL์„ ํ˜ธ์ถœํ•˜๋ฉด Controller์—์„œ ์š”์ฒญ์„ ๋ฐ›๊ณ  ๋กœ์ง์ด๋‚˜ ๋ฐ์ดํ„ฐ๊ฐ€ ํ•„์š”ํ•˜๋ฉด Model์—์„œ ํ˜ธ์ถœํ•˜์—ฌ ๋ชจ๋ธ์— ๊ตฌ์„ฑ๋œ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง(Service)์ด๋‚˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค(Repository)์— ์ ‘๊ทผํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.

- ๊ฐ€์ ธ์˜จ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์‹œ Controller -> VIEW๋กœ ๋ณด๋‚ด๋ฉฐ ์ตœ์ข…์ ์œผ๋กœ ์œ ์ €์—๊ฒŒ ๋ฐ์ดํ„ฐ์™€ ๋กœ์ง์ด ์ ์šฉ๋œ ํ™”๋ฉด์„ ์ œ๊ณต.

 

โ–ถ  MVCํŒจํ„ด ์‚ฌ์šฉ์ด์œ 

- View์™€ Controller๋ฅผ ๋ชจ๋‘ ๊ตฌํ˜„ํ•œ ๊ตฌ์กฐ์ธ Model 1 ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๊ตฌ์กฐ์˜ ํ๋ฆ„์ด ๋‹จ์ˆœํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐœ๋ฐœ์ด ์‰ฝ๊ณ  ์ค‘์†Œํ˜• ํ”„๋กœ์ ํŠธ์— ์ ํ•ฉํ–ˆ์ง€๋งŒ ํ”„๋กœ์ ํŠธ์˜ ๊ทœ๋ชจ๊ฐ€ ์ปค์งˆ์ˆ˜๋ก ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ์–ด๋ ต๊ณ , view์™€ Controller๊ฐ€ ํ•ฉ์ณ์ ธ ์žˆ์–ด ๋ฐฑ์—”๋“œ์™€ ํ”„๋ŸฐํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž์˜ ํ˜‘์—…์— ์–ด๋ ค์›€์ด ์žˆ์–ด controller์™€ view๋ฅผ ๋ถ„๋ฆฌํ•˜์—ฌ ์‚ฌ์šฉ!

- ์—ญํ• ์— ๋”ฐ๋ฅธ ๋ถ„๋ฆฌ๋กœ ์œ ์ง€๋ณด์ˆ˜์„ฑ, ํ™•์žฅ์„ฑ, ์œ ์—ฐ์„ฑ์ด ์ฆ๊ฐ€ํ•˜๊ณ  ,์ฝ”๋“œ์˜ ์ค‘๋ณต์ด ์‚ฌ๋ผ์ง€๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉํ•œ๋‹ค.

 

โ–ถ  MVCํŒจํ„ด ํŠน์ง•

- ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ ์ฒ˜๋ฆฌ ๋ถ€๋ถ„๊ณผ ์‘๋‹ต์ฒ˜๋ฆฌ ๋ถ€๋ถ„์„ ๋ถ„๋ฆฌํ•˜์—ฌ ๊ฐ ๋กœ์ง์„ ๋…๋ฆฝ์ ์œผ๋กœ ์ˆ˜ํ–‰๊ฐ€๋Šฅ

- ๋ฐฑ์—”๋“œ ํ”„๋ก ํŠธ์—”๋“œ ๋ถ„๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅ

 


Controller

 

โ–ถ Controller๋ž€?

- Spring 4๊ณ„์ธต ๊ตฌ์กฐ์—์„œ Presentation Layer(tier)๋ฅผ ๋‹ด๋‹น

 

โ–ถ Controller์˜ ์—ญํ• 

- ์‚ฌ์šฉ์ž์˜ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•œ ํ›„ ์ง€์ •๋œ ๋ทฐ์— ๋ชจ๋ธ ๊ฐ์ฒด๋ฅผ ๋„˜๊ฒจ์ฃผ๋Š” ์—ญํ• 

- ์‚ฌ์šฉ์ž์˜ ์š”์ฒญ์„ ์ „๋‹ฌ๋ฐ›์•„ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋Š” Service ํ˜ธ์ถœ

- Service์—์„œ ์ฒ˜๋ฆฌํ•œ ๋‚ด์šฉ์€ ๋‹ค์‹œ Controller ๋ฐ›์•„ client์—๊ฒŒ ๋„˜๊ฒจ์ค€๋‹ค.

 

โ–ถ Controller ์˜ˆ์‹œ์ฝ”๋“œ

@RestController
public class OrderController {

	private final OrderService service;
    	private final OrderRepository repository;


    @GetMapping("/order")
    public List<Order> getOrders() {
    	List<Order> orders = service.findOrders();
        return orders;
    }
}

Service

 

โ–ถ Service๋ž€?

- Spring 4๊ณ„์ธต ๊ตฌ์กฐ์—์„œ Application Layer(์‘์šฉ ๊ณ„์ธต) ๋‹ด๋‹นํ•˜๋ฉฐ, ์„œ๋น„์Šค์˜ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์ •์˜ํ•œ๋‹ค.

 

โ–ถ Service์˜ ์—ญํ• 

- Controller์— ์˜ํ•ด ํ˜ธ์ถœ๋˜์–ด ์‹ค์ œ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง๊ณผ ํŠธ๋žœ์žญ์…˜ ์ฒ˜๋ฆฌ

- ์‚ฌ์šฉ์ž์˜ ์š”๊ตฌ์‚ฌํ•ญ ์ฒ˜๋ฆฌ(ํ”„๋กœ์ ํŠธ๊ฐ€ ์ปค์งˆ์ˆ˜๋ก ๋น„๋Œ€ํ•ด์ง)

- DB ๋ฐ์ดํ„ฐ๊ฐ€ ํ•„์š”ํ•  ๋•Œ Repository ํ˜ธ์ถœ

 

โ–ถ Service ์˜ˆ์‹œ์ฝ”๋“œ

 public List<Order> findOrders(){
        return (List<Order>) orderRepository.findAll();
    }

Repository

 

โ–ถ Repository๋ž€?

- Spring 4๊ณ„์ธต ๊ตฌ์กฐ์—์„œ ์ธํ”„๋ผ ๊ณ„์ธต์„ ๋‹ด๋‹นํ•˜๋ฉฐ, ์ฃผ๋กœ ์™ธ๋ถ€์™€์˜ ํ†ต์‹ ์„ ๋‹ด๋‹นํ•œ๋‹ค.

 

โ–ถ Repository์˜ ์—ญํ• 

- Service์— ์˜ํ•ด ํ˜ธ์ถœ๋˜์–ด DB์—์„œ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ํ›„ ๋ฐ˜ํ™˜

- Entity์— ์˜ํ•ด ์ƒ์„ฑ๋œ DB์— ์ ‘๊ทผํ•˜๋Š” ๋ฉ”์„œ๋“œ๋“ค์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ์ธํ„ฐํŽ˜์ด์Šค

- ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ž์›์„ ๊ด€๋ฆฌํ•˜๊ณ  CRUD ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•œ๋‹ค.

 

โ–ถ Repository ์˜ˆ์‹œ์ฝ”๋“œ

public interface OrderRepository extends CrudRepository<Order, Long> {
    
}