도메인 로직에서 저장소에 접근하기 위해서 쿼리를 전송하는 것은 좋지 않다. 특정 데이터베이스에 종속된다. RDBMS도 MySQL, MsSQL, Oracle 다 쿼리 스펙이 다르다. 그래서 데이터 소스(DB)와 커뮤니케이션할 추상 레이어를 둔다. 데이터베이스의 변경 여지가 있기 때문에 추상화하기도 한다. MySQL -> MsSQL 전환, RDBMS -> NoSQL로의 전환도 언젠가 할지도 모른다.
물론 DB 전환을 염두하고 추상화하지 말라는 뉘앙스의 글도 있다: "Database Abstraction Layers Must Die!"라는 글을 읽고
아무튼. Repository Pattern은 데이터의 중앙화와 API의 일관성을 유지하고 중복 코드를 제거해 준다.
MSDN의 Repository Pattern을 번역한 글:
http://vandbt.tistory.com/27
개념적인 글이라서 코드가 있는 MSDN 글과 보면 좋다:
https://docs.microsoft.com/ko-kr/aspnet/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application
UnitOfWork 패턴까지 이어지는 글이다.
Repository Pattern에는 두 종류가 있다. Generic Repository와 Specific Repository
코드 중복을 제거하고 일관성 유지를 강조하는 Generic Repository와 유연성을 강조하는 Specific Repository.
이 글에서 이어진다.