Generic Repository vs. Specific Repository

Generic Repository vs. Specific Repository

https://stackoverflow.com/questions/1230571/advantage-of-creating-a-generic-repository-vs-specific-repository-for-each-obje

  • 점수를 많이 받은 답은 Specific Repository를 더 선호한다. 그 이유는 아래와 같다.
  • 모든 엔티티가 저장소를 가지는 것은 아니기 때문이다.
  • 하지만 베이스 레포지토리 (abstract class)는 사용한다.
  • a repository is a part of the domain being modeled, and that domain is not generic. Not every entity can be deleted, not every entity can be added, not every entity has a repository
    레포지토리는 모델링 되는 도메인의 일부분이며, 그 도메인은 generic 하지 않다. 모든 엔티티가 삭제되거나 추가되는 것이 아니며, 모든 엔티티가 레파지토리를 가지는 것은 아니다.

Generic Repository?

  • Repository를 규격화 한다.
  • Repository<User>, Repository<Comment> 처럼 Entity 클래스를 Generic Type으로 받는다.
  • 장점으로 모든 Repository는 일관된 인터페이스를 가진다.

Specific Repository?

  • Repository를 테이블마다 구현한다.
    • UserRepository, CommentRepository
  • CRUD 뿐만 아니라 테이블별 각각 다른 메소드를 구현할 수 있다.
    • UserRepository.addUser, CommentRepository.deleteShortComment 처럼..
  • 코드 양은 많아 지겠지만 Generic 보다 더 유연할 듯하다.

생각해 본 것들:

대표적으로 C#의 Entity Framework. 대부분 DB 프레임워크는 어노테이션을 이용한다.

public class Blog
{
    [Key]
    public int PrimaryTrackingKey { get; set; }
    public string Title { get; set; }
    public string BloggerName { get; set;}
    public virtual ICollection<Post> Posts { get; set; }
}

내가 위 코드를 동작케 한다면 다음과 같은 규칙을 가질 것이다:

  1. 프로퍼티 이름 = 테이블 컬럼 이름
  2. [Key] 어노테이션은 Primary Key가 되는데, 데이터베이스 PK, Unique, Auto increment 속성을 가진다.
  3. 언어의 타입 int, string 등을 데이터베이스 타입에 적절히 매핑해야 함

만약 어노테이션을 사용하지 않고, 자바스크립트로 구현한다면?

  1. 프로퍼티 이름 = 테이블 컬럼 이름은 가능
  2. PK가 될 컬럼(프로퍼티)는 어떻게?
  3. 타입은 어떻게 하나.

위 문제를 해결하기 위해서 static 변수에 pk, type 등 정보를 저장해야 할 거 같다.

Specific Repository를 구현한다면 어노테이션 없이도 복잡하지 않게 구현할 수 있을 거 같다.

각 메서드에서 쿼리를 만들 거고(쿼리 빌더를 쓰던간에), 어노테이션 없는 Entity 클래스도 만들 수 있다!

results for ""

    No results matching ""