본문 바로가기

Dev.../소프트웨어 아키텍처

[SA강좌] Part 4-5 OR Mapping 패턴 개요

OR Mapping 패턴 개요

OR Mapping 패턴은 Data Source Architecture 패턴과 OR Mapping 행동 패턴, OR Mapping 구조 패턴, OR 메타데이터 매핑 패턴으로 분류된다.

그림 -4. OR 매핑 패턴

Datasource Architecture Pattern은 도메인 객체와 데이터베이스가 연결되는 방법을 결정한다. OR Mapping 행동 패턴은 데이터베이스에서 정보를 load하는 방식과 도메인 객체의 정보를 save하는 방식을 결정한다. OR Mapping 구조 패턴은 객체 모델에서 관계형 데이터베이스 모델로 변환하는 방식을 결정한다. OR 메타데이터 매핑 패턴은 OR 매핑에 대한 코드를 메타데이터를 사용하여 자동으로 생성하는 방법을 결정한다.

Datasource Architecture Pattern은 Table Data Gateway, Row Data Gateway, Active Record, Data Mapper로 나누어진다.

그림 -5. Datasource Architecture Pattern

Table Data Gateway는 한 인스턴스가 테이블과 연결된다. Row data gateway는 한 레코드가 인스턴스와 연결된다. 도메인 로직 객체가 테이블 매핑 로직도 가지고 있는 경우에는 Active Record를 쓴다. 별도의 OR Mapping Layer를 만들거나 OR Mapping 툴을 사용하는 경우에는 Data Mapper를 사용한다.

OR Mapping 행동 패턴은 Unit of Work와 Identity Map, Lazy Load 등이 있다.

그림 -6. OR Mapping 행동 패턴

Identity Map은 객체가 오직 한번만 메모리에 load되도록 한다. Unit of Work는 트랜잭션에 의해 영향을 받는 모든 객체의 리스트를 관리한다. Lazy Load는 Master-detail 성격의 데이터에 대해 Master만 가져오고 추후 detail 데이터가 필요한 경우 메모리에 load할 때 사용한다.

OR Mapping 구조 패턴은 Identity Field, Foreign Key Mapping, Association Table Mapping, Dependent Maping, Embedded Value, Serialized LOB, Single Table Inheritance, Class Table Inheritance, Concrete Table Inheritance, Inheritance Mapper등이 있다.

그림 -7. OR Mapping 구조 패턴

Foreign Key Mapping은 객체 사이의 참조를 테이블의 foreign key에 매핑한다. Identity Field는 key를 사용하여 객체와 매핑되는 테이블을 찾는다. Embedded Value는 객체의 몇 개의 field를 embedded 객체를 사용하여 표현한다. Association Table Mapping은 Association을 테이블에 매핑한다. Depedent Mapping은 collection을 가져오는 로직을 별도의 객체로 처리할 경우 사용한다.

복합 객체를 저장하는 경우는 Serialized LOB를 사용한다.

상속 관계는 concrete table inheritance, class table inheritance, inheritance mapper, single table inheritance를 사용하여 구현한다.

OR 메타테이터 매핑 패턴은 Metadata Mapping, Query Object, Repository로 나누어 진다.

그림 -8. OR 메타데이터 매핑 패턴

인터프리터 방식으로 실행 중에 쿼리를 생성하여 실행할 경우는 query object를 사용한다. 메타 데이터를 사용하여 OR mapping 코드를 생성하는 경우는 metadata mapping을 사용한다. 사용자의 action에 매핑되는 쿼리를 찾아서 실행하는 경우에는 repository를 사용한다.