백엔드에서 관심사에 따라 컴포넌트를 Repository,Service,Controller로 나눌 수 있다.
각각 담당하는 역할은 아래와 같다
prisma를 통해 DB CRUD 작업
Repository를 통해 불러온 데이터를 가공하여 반환
Service에서 가공한 데이터를 사용하는 프레임워크(express,nest)를 통해서 응답한다
의존성 주입 순서는 작성된 순서와 동일하다.
Repository → Service → Controller
export default class QuotesRepository {
constructor(private prismaClient:PrismaClient){}
}
의존성 주입이란 객체가 필요로 하는 외부 자원을 직접 생성하지 않고 외부에서 제공받는 방식이다.
class에서 constructor를 통해 의존성을 주입할 수 있다. constructor는 함수의 파라미터와 같은 개념으로 class를 통해 객체를 생성할 때 입력할 수 있다.
prismaClient = new PrismaClient();
quoteRepository = new QuotesRepository(prismaClient);